js爲Array數組添加remove函數
js默認是沒有remove函數的,會出現
Uncaught TypeError: arr.remove is not a function
解決方法大家應該都會,但是爲了更方便,可以添加prototype,具體如下:
Array.prototype.remove = function () {
for (var i = 0; i < arguments.length; i++) {
var ele = arguments[i];
var index = this.indexOf(ele);
if (index > -1) {
this.splice(index, 1);
}
}
};
var arr = [1,2,3,4,5];
arr.remove(1);
alert(arr);