splice常用通过方法

Posted 路上

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了splice常用通过方法相关的知识,希望对你有一定的参考价值。

/*
从数组中删除指定定数据
var somearray = ["mon", "tue", "wed", "thur"]
somearray.removeByValue("tue");
//somearray will now have "mon", "wed", "thur"
*/
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.exists ("tue");
//somearray will return true
*/
Array.prototype.exists = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) {
return true;
}
}
return false;
}

以上是关于splice常用通过方法的主要内容,如果未能解决你的问题,请参考以下文章

Vue:数组splice方法的使用

前端开发:数组多个元素删除

JavaScript 使用 splice 方法删除数组元素可能导致的问题

数组常用slice和splice的区别

js常用的array方法

常用的数组方法