简体   繁体   中英

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

I have a js 'associative' array, with

array['serial_number'] = 'value'

serial_number and value are strings. eg 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

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).

2021 Update

Since ES6, properties with string keys are enumerated in insertion order. Here's a nice summary . My original answer from 2010 was correct at the time and is preserved below:

Original answer

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 Array instead, either to store name-value objects:

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). 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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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