javascaipt中数组元素删除方法delete和splice

一、用delete 删除数组中元素(少用)

说明:delete: 只是被删除的元素变成了 undefined ,即数组长度不变

//定义数组
var array=["aa","dd","cc","aa"];
//方法1:delete删除数组元素
delete array[1];
//输出结果 : ["aa",undefined,"cc","aa"]

输出结果:

    aa

    undefined

    cc

    aa

二、用splice删除数组的元素

说明:该方法会自动改变原始数组,即每用一次splice(i,1)数组长度自动减1

 var array = ["aa","dd","cc","aa"];
   //方法2:删除数组元素
    array.splice(1,1);
   //输出结果:["aa","cc","aa"]

输出结果:

    aa

    cc

    aa

 

posted @ 2019-12-06 20:52  月半大熊猫  阅读(468)  评论(0编辑  收藏  举报