How do I unset an element in an array in javascript?

前端 未结 6 461
闹比i
闹比i 2020-12-01 03:13

How do I remove the key \'bar\' from an array foo so that \'bar\' won\'t show up in

for(key in foo){alert(key);}
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 03:57

    An important note: JavaScript Arrays are not associative arrays like those you might be used to from PHP. If your "array key" is a string, you're no longer operating on the contents of an array. Your array is an object, and you're using bracket notation to access the member named . Thus:

    var myArray = [];
    myArray["bar"] = true;
    myArray["foo"] = true;
    alert(myArray.length); // returns 0.
    

    because you have not added elements to the array, you have only modified myArray's bar and foo members.

提交回复
热议问题