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

后端 未结 10 2080
离开以前
离开以前 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.

    0 讨论(0)
  • 2020-12-07 15:45

    Compiling other answers:

    Object

    var test = {};
    

    When using a number as a new property's key, the number turns into a string:

    test[2300] = 'Some string';
    console.log(test['2300']);
    // Output: 'Some string'
    

    When accessing the property's value using the same number, the number is turned into a string again:

    console.log(test[2300]);
    // Output: 'Some string'
    

    When getting the keys from the object, though, they aren't going to be turned back into numbers:

    for (var key in test) {
        console.log(typeof key);
    }
    // Output: 'string'
    

    Map

    ECMAScript 6 allows the use of the Map object (documentation, a comparison with Object). If your code is meant to be interpreted locally or the ECMAScript 6 compatibility table looks green enough for your purposes, consider using a Map:

    var test = new Map();
    test.set(2300, 'Some string');
    console.log(test.get(2300));
    // Output: 'Some string'
    

    No type conversion is performed, for better and for worse:

    console.log(test.get('2300'));
    // Output: undefined
    test.set('2300', 'Very different string');
    console.log(test.get(2300));
    // Output: 'Some string'
    
    0 讨论(0)
  • 2020-12-07 15:49

    If the use case is storing data in a collection then ECMAScript 6 provides the Map type.

    It's only heavier to initialize.

    Here is an example:

    const map = new Map();
    map.set(1, "One");
    map.set(2, "Two");
    map.set(3, "Three");
    
    console.log("=== With Map ===");
    
    for (const [key, value] of map) {
        console.log(`${key}: ${value} (${typeof(key)})`);
    }
    
    console.log("=== With Object ===");
    
    const fakeMap = {
        1: "One",
        2: "Two",
        3: "Three"
    };
    
    for (const key in fakeMap) {
        console.log(`${key}: ${fakeMap[key]} (${typeof(key)})`);
    }
    

    Result:

    === With Map ===
    1: One (number)
    2: Two (number)
    3: Three (number)
    === With Object ===
    1: One (string)
    2: Two (string)
    3: Three (string)
    
    0 讨论(0)
  • 2020-12-07 15:53

    Use an object instead of an array. Arrays in JavaScript are not associative arrays. They are objects with magic associated with any properties whose names look like integers. That magic is not what you want if you're not using them as a traditional array-like structure.

    var test = {};
    test[2300] = 'some string';
    console.log(test);
    
    0 讨论(0)
  • 2020-12-07 15:55

    Try using an Object, not an Array:

    var test = new Object(); test[2300] = 'Some string';
    
    0 讨论(0)
  • 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"]);
    
    0 讨论(0)
提交回复
热议问题