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=['a','s','d','f'];
console.log(arr);
arr.remove('s');
console.log(arr);
微信扫码查看本文
发表评论