时间:2021-07-01 10:21:17 帮助过:26人阅读
Js代码
function removeByValue(arr, val) { for(var i=0; i<arr.length; i++) { if(arr[i] == val) { arr.splice(i, 1); break; } } } var somearray = ["mon", "tue", "wed", "thur"] removeByValue(somearray, "tue"); //somearray will now have "mon", "wed", "thur"
为数组对象增加相应的的方法
Js代码
Array.prototype.removeByValue = function(val) { for(var i=0; i<this.length; i++) { if(this[i] == val) { this.splice(i, 1); break; } } } var somearray = ["mon", "tue", "wed", "thur"] somearray.removeByValue("tue"); //somearray will now have "mon", "wed", "thur"