js 删除数组的指定元素

//为数组对象增加相应的的方法,直接调用数组的removeByValue方法即可删除指定元素
Array.prototype.removeByValue = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) {
this.splice(i, 1);
break;
}
}
}

var arry = ['1', '2', '3'];
console.log(arry);//["1", "2", "3"]


arry.removeByValue('4');
console.log(arry);//["1", "2", "3"]


arry.removeByValue('2');
console.log(arry);//["1", "3"]

posted @ 2017-03-28 14:09  #天行健#  阅读(519)  评论(0编辑  收藏  举报