JavaScript刪除數組中的項 delete vs splice

var myArray=["a","b","c"];
delete myArray[0];
for(var i=0,j=myArray.length;i<j;i++){
    console.log(myArray[i]);
    /*
    undefined
    b
    c
    */
}

var myArray2=["a","b","c"];
myArray2.splice(0,1);
for(var i=0,j=myArray2.length;i<j;i++){
    console.log(myArray2[i]);
    /*
    b
    c
    */
}

上面的代碼已經說明區別了,一個是設置為undefined,一個是真正的刪除了。


所屬標簽

無標簽

25选5玩法中奖