简体   繁体   English

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

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

I have a js 'associative' array, with我有一个 js 'associative' 数组,与

array['serial_number'] = 'value'

serial_number and value are strings. serial_number 和 value 是字符串。 eg array['20910930923'] = '20101102'例如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.现在我想访问“已排序”数组的第一个 KEY。 How do I do it?我该怎么做? I can't think I need an iteration with我不能认为我需要迭代

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 不支持关联数组(这就是为什么我把它放在标题中的高逗号中)。

2021 Update 2021 更新

Since ES6, properties with string keys are enumerated in insertion order.从 ES6 开始,具有字符串键的属性按插入顺序枚举。 Here's a nice summary .这是一个很好的总结 My original answer from 2010 was correct at the time and is preserved below:我 2010 年的原始答案当时是正确的,并保留在下面:

Original answer原答案

JavaScript object properties are specified to have no order, much though many people wish it were different . JavaScript 对象属性被指定为没有顺序,尽管很多人希望它是不同的 If you need ordering, abandon any attempt to use an object and use an Array instead, either to store name-value objects:如果您需要排序,请放弃任何使用对象的尝试,而是使用Array来存储名称-值对象:

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'];

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]];

Actually JavaScript doesn't support associative arrays, so you can't loop through it in an implied order (eg you can't access it via the indexer property array[0] won't access the first element in your object).实际上 JavaScript 不支持关联数组,因此您不能以隐含的顺序遍历它(例如,您无法通过索引器属性访问它, array[0]将不会访问您对象中的第一个元素)。 The syntax is what makes it look like it does, but in reality it doesn't.语法使它看起来像,但实际上并非如此。 So you have no "Order" to your objects.所以你的对象没有“订单”。

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. Javascript 没有,也不支持关联数组。 However… All arrays in Javascript are objects and Javascript's object syntax gives a basic emulation of an associative Array.然而……Javascript 中的所有数组都是对象,Javascript 的对象语法提供了对关联数组的基本模拟。 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.示例中的“person”元素成为 Array 对象的属性和方法的一部分,就像 .length、.sort()、.splice() 以及所有其他内置属性和方法一样。

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 中的值。

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

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