Using an integer as a key in an associative array in JavaScript

后端 未结 10 2082
离开以前
离开以前 2020-12-07 15:34

When I create a new JavaScript array, and use an integer as a key, each element of that array up to the integer is created as undefined.

For example:

v         


        
10条回答
  •  时光说笑
    2020-12-07 15:56

    Use an object, as people are saying. However, note that you can not have integer keys. JavaScript will convert the integer to a string. The following outputs 20, not undefined:

    var test = {}
    test[2300] = 20;
    console.log(test["2300"]);
    

提交回复
热议问题