如何在js数组中删除指定元素

更新时间:02-01 教程 由 沉苍 分享

如何在js数组中删除指定元素?

首先需要找到元素的下标:

var array = [1, 2, 3];

var index = array.indexOf(2);

使用splice函数进行移除:

if (index > -1) {

array.splice(index, 1);

}

splice函数的第二个参数指删除的数目。splice直接修改原数组,并把删除的所有元素以另一个新数组的方式返回。

声明:关于《如何在js数组中删除指定元素》以上内容仅供参考,若您的权利被侵害,请联系13825271@qq.com
本文网址:http://www.25820.com/tutorial/14_2253101.html