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 instanciating 2299 empty indices?
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:
You can just use an object:
As people say javascript will convert an string of number to integer so 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:
and add the values as array works:
this will give you:
After that you can access it like array in other languages getting the key:
I hope this is useful! I have tested and works.
Sometimes i use a prefixes for my keys. For example:
Now you have no Problem accessing them.