3

I need to create an associative array in javascript with an integer key as follows;

a["10"] = "ten";

but when i create an array, it puts the value to the 10th index of the array and it creates an array with the length 11. I want it to be a key value pair. I know this can be done by using objects but i need an array only.

3
  • arrays were not meant to do this, so why do you need an array? Perhaps objects have the functionality you are looking for, but you just aren't aware. Commented Jan 14, 2013 at 4:51
  • i am using javascript with a project in SAPUI5, and i need arrays to populate in the dropdowns and the dropdowns does not support the objects. i need arrays only.. :( Commented Jan 14, 2013 at 4:53
  • 2
    Javascript arrays are Objects. The "indexes" are just numeric property names, the length property is defined as being the highest index plus one, it is not a count of the indexes. Commented Jan 14, 2013 at 5:34

2 Answers 2

8

JavaScript does not have associative arrays. The only way to do this in JavaScript is to use objects:

var a = {
    '10': 'ten'
};
Sign up to request clarification or add additional context in comments.

15 Comments

-1 JavaScript does have Associative Arrays (aka Map aka Dictionary); albeit in a limited form (i.e. Objects) that coerce all keys to strings. While the term feels fairly antiquated, it is valid and is commonplace some languages like Perl (and is abused in other languages like PHP).
@Daksh - Nope. Can you tell us why you need it to be an array? Maybe we can work around that.
@Daksh - there is no such thing as a sparse/associative array in javascript. You can have an array or an object. Those are your choices. You can want something different, but it doesn't exist.
@Cam I disagree. This answer starts with an incorrect statement.(Also, it does not explain why using "10" in as an Array property is identical as using 10, so there is nothing to counter-balance my nit.)
@jfriend00—I think the term "sparse" is OK and understood as an array where some indexes are elided, e.g. [0,1,,,4] or are otherwise undefined.
|
2

ECMAScript does have Associated Arrays1 - Objects (and by extension, Arrays) are an example

However, some properties of Arrays are treated specially:

Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P ..

.. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index ..

Thus, given arr = [], the expressions arr["1"] and arr[1] refer to the same property name. Since P (the property name) is "1" and length is 0 from above, then assignment to such property will set arr.length to ToUint32(P)+1, or 2.

It is not possible to change this behavior. If you wish to not have a special length property, then use a "normal" Object instead of an Array. However, many of the Array.prototype functions can be used with arbitrary objects (with some implementation quirks aside) that have a length property and an Object can be created such that it uses Array.prototype as its own prototype.

All that being said, the post does not say what the real issue is. Instead of supposing that it must be done in that particular manner, consider explaining what the intent is: e.g. why a["10"]? And what is wrong if there are "11 items" if the object will be used in a List?


1 Please read the article before debating this statement: the term "Array" in the name does not imply an ordered sequence nor does it preclude an additional notion of a Length or the use of Hashing, etc. If you are going by a different definition, make sure to specify what it is and what the desired behavior is for a given operation.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.