您的位置:首页 > Web前端 > JavaScript

js数组删除指定元素

2017-10-21 11:10 344 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var array = ["111", "222", "333", "444", "555", "666"];
var indexList = [0, 5, 6];
var indexList1 = [5];
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(indexList1);
console.log("after delete: " + array);
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: