125

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:

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

will output 2298 undefined's and one 'Some string'.

How should I get JavaScript to use 2300 as a string instead of an integer, or how should I keep it from instantiating 2299 empty indices?

11 Answers 11

150

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"]);

Sign up to request clarification or add additional context in comments.

12 Comments

+1 Note that this is even true of Arrays! see stackoverflow.com/questions/1450957/…
@bobince: Internally, sure. However, logically, arrays have integer "keys".
Please note, that using integer as key will change the length of your array. You should definitely use Object instead. I just wanted to use facebook id as the key, and JSON.stringify would crash my machine ;)
@LightnessRacesinOrbit The internal details can still leak out and bite you though. See this simplified version of what I ran into today: jsfiddle.net/cincodenada/pseujLex/2 It may seem contrived when reduced, but was a sensical part of a larger script (and is a bit less contrived in CoffeeScript: jsfiddle.net/cincodenada/oojr7Ltn/2). This seeming implementation detail cost me a good bit of bug-hunting today.
A little note for non-whole numbers: 0.25 and .25 resolve to the same string "0.25". So if you're using fractional keys, you can retrieve the property of a numerically set key 0.25 using 0.25, .25, "0.25" but not ".25".
|
37

You can just use an object:

var test = {}
test[2300] = 'Some string';

2 Comments

Still gets cast to string.
@drew010 yes, Javascript's objects allow indexing with strings only.
25

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)

Comments

25

As people say, JavaScript will convert a number to a string, so it is not possible to use directly on an associative array, but objects will work for you in similar way I think.

You can create your object:

var object = {};

And add the values as array works:

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

This will give you:

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

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

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

Comments

12

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'

Comments

4

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);

4 Comments

They can be associative arrays, but only because they are also objects which can have named properties set. But this just makes things ridiculously confusing, and so yes, objects are much better to use.
Arrays can never be associative Graza. If you try to use keys in an array and then iterate over them you'll notice you're also iterating through all the default methods and properties of arrays -> not very desirable.
@Swizec - exactly why I said "ridiculously confusing". You can use an array as an associative array - that is as name/value pairs, but you would never want to iterate them! (I was simply pointing out a technicality, definitely not something I would at all recommend doing)
yes, but when iterating they're not in any particular order (i.e. order is not guaranteed), which would be the point of numbering them, so it's a lot worse than just confusing.
3

Try using an Object, not an Array:

var test = new Object(); test[2300] = 'Some string';

2 Comments

This is definitely the way to go. This way you won't create 2300 entries long array in order to store a single string.
@Krystian JS arrays are fake arrays. Run var a = []; a[Math.pow(2, 30)] = 'hello'; and you won't see the browser/memory usage shoot up by over a gigabyte, but you will see a.length is 1073741824. VMs clearly store some "arrays" using some other data structure, I'm guessing simply a hashtable, at least if they're sufficiently sparse.
1

Get the value for an associative array property 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"}
];

Push items to the array:

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

Loop 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
    }
}

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 get around the associative array limitation and have a property name be an integer.

NOTE: The associative array in my example is the JSON content you would have if you serialized a Dictionary<string, string>[] object.

1 Comment

-1 : even if this an old answer, this does by no means answer the request for a int used as a key, as 1) strings are used and 2) these are not keys, since there’s a need for a loop to determine which entry is the desired one.
1

Simple solution if you would rather use an array. When adding the number just preface it with a letter.

e.g.

let ctr = 3800;
let myArray=[];
myArray["x" + ctr.toString()]="something";
myArray["x" + (ctr+1).toString()]="another thing";

Then just add the "x" in access routines that call the number as an index. e.g.: console.log( myArray["x3800"] ); or: console.log( myArray["x"+ numberOfYourChoice.toString()] );

Comments

0

Use an object - with an integer as the key - rather than an array.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.