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

后端 未结 10 2129
离开以前
离开以前 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:44

    Sometimes I use a prefixes for my keys. For example:

    var pre = 'foo',
        key = pre + 1234
    
    obj = {};
    
    obj[key] = val;
    

    Now you don't have any problem accessing them.

提交回复
热议问题