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.
- 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
2 Answers
Reset to default 8JavaScript 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 ifToString(ToUint32(P))
is equal toP
.... 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
版权声明:本文标题:javascript - Create an associative array with integer keys - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741983558a2408540.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论