数组属于对象,对象有delete方法,可以删除数组元素。
var numbersArr = [1,2,3,4,5,6];
delete numbersArr[2];
//numbersArr结果为[1,2,undifined,4,5,6]
image.png
数组提供splice方法
var numbersArr = [1,2,3,4,5,6];
numbersArr.splice(2,1);//第一个参数为数组序号,第二个参数为删除个数
//numbersArr结果为[1,2,4,5,6],被删元素后面的元素序号都发生前移
image.png
另外,splice方法还可以在任意位置插入元素:
let arr = [1,4,3,"a","c","e"];
arr.splice(2,0,"ok","fine",4,6)//[]
console.log(arr);//[1, 4, "ok", "fine", 4, 6, 3, "a", "c", "e"]
image.png
网友评论