Are Javascript arrays sparse?

后端 未结 7 2283
情书的邮戳
情书的邮戳 2020-11-22 08:12

That is, if I use the current time as an index into the array:

array[Date.getTime()] = value;

will the interpreter instantiate all the elem

7条回答
  •  [愿得一人]
    2020-11-22 08:52

    You could avoid the issue by using a javascript syntax designed for this sort of thing. You can treat it as a dictionary, yet the "for ... in ... " syntax will let you grab them all.

    var sparse = {}; // not []
    sparse["whatever"] = "something";
    

提交回复
热议问题