var array = ["111", "222", "333", "444", "555", "666"];
var indexList = [0, 5, 2];
Array.prototype.del = function(indexList) {
function isValidate(number) {
if(isNaN(number) && number > this.length) {
return false;
}
}
if(indexList instanceof Array) {
indexList.sort(function(x, y) {
if( x > y) {
return 1;
} else {
return -1;
}
});
var lastIndex = indexList[indexList.length - 1];
isValidate(lastIndex);
for(var i = 0; i < indexList.length; i++) {
var n = i;
if(n > 0) {
indexList[i] = indexList[i] - n;
}
this.splice(indexList[i], 1);
n++;
}
} else {
isValidate(indexList);
this.splice(indexList, 1);
}
}
console.log("before delete: " + array);
array.del(indexList);
console.log("after delete: " + array);
刪除一個數組中指定位置的元素
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.