admin管理员组

文章数量:1315792

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.

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.

Share Improve this question edited Jan 14, 2013 at 4:56 MysticMagicϡ 28.8k16 gold badges75 silver badges125 bronze badges asked Jan 14, 2013 at 4:47 Vishal AroraVishal Arora 5433 gold badges8 silver badges19 bronze badges 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. – Cam 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.. :( – Vishal Arora 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. – RobG Commented Jan 14, 2013 at 5:34
Add a ment  | 

2 Answers 2

Reset to default 8

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

var a = {
    '10': 'ten'
};

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.

本文标签: javascriptCreate an associative array with integer keysStack Overflow