Javascript: using integer key in associative array?

When I create a new javascript array and use an integer as a key, each element of this array up to an integer is created as undefined. eg:

var test = new Array(); test[2300] = 'Some string'; console.log(test); 

will output 2298 undefined and one line "Some string".

How do I get javascript to use 2300 as a string instead of an integer, or how do I save it from instangiating 2299 empty indices?

+63
javascript associative-array
Jan 04 '10 at 23:03
source share
9 answers

Use the object as people say. However, note that you cannot have integer keys. JavaScript converts an integer to a string . The following outputs are 20, not undefined:

 var test = {} test[2300] = 20; console.log(test["2300"]); 
+99
Jan 04 '10 at 23:12
source share

You can simply use the object:

 var test = {} test[2300] = 'Some string'; 
+27
Jan 04 '10 at 23:05
source share

As people say, javascript converts a string of numbers to integer, so it is impossible to use directly in an associative array, but objects will work for you in a similar way. I think.

You can create your own object:

 var object = {}; 

and add the values โ€‹โ€‹as an array:

 object[1] = value; object[2] = value; 

this will give you:

 { '1':value, '2':value } 

After that, you can access it as an array in other languages, getting the key:

 for(key in object) { value = object[key] ; } 

Hope this is helpful! I tested and works.

+16
Mar 24 '14 at 12:51
source share

If the use case stores data in a collection, then ES6 provides the Map type.

It is only harder 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) 
+3
Dec 25 '16 at 17:39
source share

Try using an object, not an array:

 var test = new Object(); test[2300] = 'Some string'; 
+2
Jan 04 '10 at 23:05
source share

Use an object instead of an array. Arrays in JavaScript are not associative arrays. These are objects with magic associated with any properties whose names are similar to integers. This magic is not what you want unless you use them as a traditional array-like structure.

 var test = {}; test[2300] = 'some string'; console.log(test); 
+2
Jan 04 '10 at 23:05
source share

Get the value for a property of an associative array when the property name is an integer:

Starting with an associative array, where the property names are integers:

 var categories = [ {"1":"Category 1"}, {"2":"Category 2"}, {"3":"Category 3"}, {"4":"Category 4"} ]; 

Move items to array:

 categories.push({"2300": "Category 2300"}); categories.push({"2301": "Category 2301"}); 

Scroll through the array and do something with the property value.

 for (var i = 0; i < categories.length; i++) { for (var categoryid in categories[i]) { var category = categories[i][categoryid]; // log progress to the console console.log(categoryid + " : " + category); // ... do something } } 

The console output should look like this:

 1 : Category 1 2 : Category 2 3 : Category 3 4 : Category 4 2300 : Category 2300 2301 : Category 2301 

As you can see, you can bypass the association array constraint and have the property name as a whole.

NOTE. The associative array in my example is json, which you would have if you serialized the Dictionary <string, string> [] object.

+2
Jun 15 '15 at 16:33
source share

Sometimes I use prefixes for my keys. For example:

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

Now you have no problems accessing them.

+1
Jun 15 '15 at 16:41
source share

Use an object - with an integer as the key, not an array.

0
Jan 04
source share



All Articles