简体   繁体   English

使用整数键创建关联数组

[英]Create an associative array with integer keys

I need to create an associative array in javascript with an integer key as follows; 我需要在javascript中使用integer键创建一个关联数组,如下所示;

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. 但是当我创建一个数组时,它将值放到数组的10th索引,并创建一个长度为11的数组。我希望它是一个键值对。 I know this can be done by using objects but i need an array only. 我知道这可以通过使用对象来完成,但我只需要一个数组。

JavaScript does not have associative arrays. JavaScript没有关联数组。 The only way to do this in JavaScript is to use objects: 在JavaScript中执行此操作的唯一方法是使用对象:

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

ECMAScript does have Associated Arrays 1 - Objects (and by extension, Arrays) are an example ECMAScript 确实关联数组 1 - 对象(以及扩展,数组)就是一个例子

However , some properties of Arrays are treated specially : 但是Arrays的某些属性是专门处理的

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 .. 当且仅当ToString(ToUint32(P))等于P属性名P (以String值的形式)是一个数组索引。

.. 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 .. .. 具体来说,每当添加名称为数组索引的属性时,如果需要,更改length属性,使其大于该数组索引的数值。

Thus, given arr = [] , the expressions arr["1"] and arr[1] refer to the same property name. 因此,给定arr = [] ,表达式arr["1"]arr[1]引用相同的属性名称。 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. 由于P (属性名称)为“1”且length为0,因此分配给此属性会将arr.length设置为ToUint32(P)+1或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. 如果你想没有特殊的length属性,然后使用一个“正常”的对象,而不是一个数组。 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. 但是,许多Array.prototype函数可以与任意对象一起使用(除了一些实现怪癖),它们具有length属性,并且可以创建Object,使得它使用Array.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: eg why a["10"]? 不要以为必须以特定方式完成,而是考虑解释意图是什么:例如为什么[“10”]? And what is wrong if there are "11 items" if the object will be used in a List? 如果对象将在List中使用,那么如果有“11项”会出现什么问题?


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. 1在讨论这个陈述之前,请阅读文章:名称中的术语“数组”并不意味着有序的序列,也不排除长度或使用哈希等的附加概念。如果你的定义不同 ,确保指定它是什么以及给定操作的期望行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM