如何访问 JavaScript 中“关联”数组的第一个键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4491272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:52:53  来源:igfitidea点击:

How do I access the first key of an ‘associative’ array in JavaScript?

javascriptkeyassociative-array

提问by transient_loop

I have a js 'associative' array, with

我有一个 js 'associative' 数组,与

array['serial_number'] = 'value'

serial_number and value are strings. e.g. array['20910930923'] = '20101102'

serial_number 和 value 是字符串。例如array['20910930923'] = '20101102'

I sorted it by value, works fine. Let's say I get back the object 'sorted';

我按值排序,效果很好。假设我取回了“已排序”的对象;

Now I want to access the first KEY of the 'sorted' array. How do I do it? I can't think I need an iteration with

现在我想访问“已排序”数组的第一个 KEY。我该怎么做?我不能认为我需要迭代

for (var i in sorted)

and just stop after ther first one...

然后在第一个之后停下来......

thanks

谢谢

edit: just to clarify, I know that js does not support associative arrays (that's why I put it in high commas in the Title).

编辑:只是为了澄清,我知道 js 不支持关联数组(这就是我在标题中将它放在高逗号中的原因)。

回答by Tim Down

JavaScript object properties are specified to have no order, much though many people wish it were different. If you need ordering, abandon any attempt to use an object and use an Arrayinstead, either to store name-value objects:

JavaScript 对象属性被指定为没有顺序,尽管很多人希望它是不同的。如果您需要排序,请放弃任何使用对象的尝试,而使用 anArray来存储名称-值对象:

var nameValues = [
    {name: '20910930923', value: '20101102'},
    {name: 'foo', value: 'bar'}
];

... or as an ordered list of property names to use with your existing object:

...或作为与现有对象一起使用的属性名称的有序列表:

var obj = {
   '20910930923': '20101102',
   'foo': 'bar'
};

var orderedPropertyNames = ['20910930923', 'foo'];

回答by psimons

Try this:

试试这个:

// Some assoc list
var offers = {'x':{..some object...}, 'jjj':{...some other object ...}};

// First element (see attribution below)
return offers[Object.keys(offers)[0]];

// Last element (thanks to discussion on finding last element in associative array :)
return offers[Object.keys(offers)[Object.keys(offers).length - 1]];

回答by kemiller2002

Actually JavaScript doesn't support associative arrays, so you can't loop through it in an implied order (e.g. you can't access it via the indexer property array[0]won't access the first element in your object). The syntax is what makes it look like it does, but in reality it doesn't. So you have no "Order" to your objects.

实际上 JavaScript 不支持关联数组,因此您不能以隐含的顺序遍历它(例如,您无法通过 indexer 属性访问它,array[0]也不会访问对象中的第一个元素)。语法使它看起来像,但实际上并非如此。所以你的对象没有“订单”。

http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

Javascript does not have, and does not support Associative Arrays. However… All arrays in Javascript are objects and Javascript's object syntax gives a basic emulation of an associative Array. For this reason the example code above will actually work. Be warned that this is not a real array and it has real pitfals if you try to use it. The 'person' element in the example becomes part of the Array object's properties and methods, just like .length, .sort(), .splice(), and all the other built-in properties and methods.

Javascript 没有,也不支持关联数组。然而…… Javascript 中的所有数组都是对象,Javascript 的对象语法提供了对关联数组的基本模拟。出于这个原因,上面的示例代码实际上可以工作。请注意,这不是一个真正的数组,如果您尝试使用它,它有真正的陷阱。示例中的“person”元素成为 Array 对象的属性和方法的一部分,就像 .length、.sort()、.splice() 以及所有其他内置属性和方法一样。

回答by Justin

Just thinking off the top of my head, but could you have another array with the key value pairs swapped?

只是在想我的头顶,但你能有另一个交换键值对的数组吗?

So the answer would be arrayKeyValueReversed['20101102'] = '20910930923';

所以答案是 arrayKeyValueReversed['20101102'] = '20910930923';

When you sort the array, use the first item (array[0]) as the key to get the value in the arrayKeyValueReversed.

对数组进行排序时,使用第一项(array[0])作为key,获取arrayKeyValueReversed中的值。