最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

I have a js 'associative' array, with

array['serial_number'] = 'value'

serial_number and value are strings. e.g. 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 mas in the Title).

I have a js 'associative' array, with

array['serial_number'] = 'value'

serial_number and value are strings. e.g. 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 mas in the Title).

Share Improve this question edited Dec 20, 2010 at 16:21 unsafe_where_true asked Dec 20, 2010 at 15:52 unsafe_where_trueunsafe_where_true 6,31016 gold badges67 silver badges123 bronze badges 4
  • 1 stackoverflow./questions/909003/… – Anders Commented Dec 20, 2010 at 15:56
  • how are you sorting the array? – Emmett Commented Dec 20, 2010 at 15:58
  • Emmett: by doing this: jamesrutherford./blog/2010/08/07/… – unsafe_where_true Commented Dec 20, 2010 at 16:02
  • Just to clear up the nomenclature - you're working with a JS object. An array in JavaScript is indexed by integers. An object is indexed by strings/identifier, and the properties can be accessed like this: obj['prop'] or like this: obj.prop, so it acts like an associative array and an object. – Skilldrick Commented Dec 20, 2010 at 16:06
Add a ment  | 

4 Answers 4

Reset to default 3

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

http://www.hunlock./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 bees 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.

发布评论

评论列表(0)

  1. 暂无评论