js使用delete 或splice删除数组的某个元素。
方法1 delete 删除数组的某个元素
//定义数组 var array=["aa","dd","cc","aa"]; //方法1:delete删除数组元素 delete array[1]; //输出结果 : ["aa",undefined,"cc","aa"]
方法2 splice删除数组的某个元素
1 var array = ["aa","dd","cc","aa"]; 2 //方法2:删除数组元素 3 array.splice(1,1); // 第一个参数为起始下标,第二个参数为删除的长度 4 //输出结果:["aa","cc","aa"]
delete 和 splice的区别
delete:数组的长度不变,被删除的元素变成了undefined。
splice: 该方法改变了原始数组。