9

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 commas in the Title).

transient_loop
  • 5,984
  • 15
  • 58
  • 117
  • 1
    http://stackoverflow.com/questions/909003/javascript-getting-the-first-index-of-an-object – Anders Dec 20 '10 at 15:56
  • how are you sorting the array? – Emmett Dec 20 '10 at 15:58
  • Emmett: by doing this: http://www.jamesrutherford.com/blog/2010/08/07/javascript-associative-array-sort/ – transient_loop Dec 20 '10 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 Dec 20 '10 at 16:06

4 Answers4

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'];
Tim Down
  • 318,141
  • 75
  • 454
  • 536
2

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]];
psimons
  • 136
  • 5
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.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.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • 2
    uhm, objects are associative arrays? – falstro Dec 20 '10 at 15:55
  • 3
    There are no associative arrays in JavaScript, it's all a big freaking object. – Anders Dec 20 '10 at 15:57
  • 1
    @Anders; potato potahto, there are no objects, they're all big freaking associative arrays. – falstro Dec 20 '10 at 15:59
  • 2
    JS objects can certainly be used as associative arrays, so the link above seems more misleading than useful (to me). – Matthew Wilson Dec 20 '10 at 15:59
  • 1
    @Matthew; I'd even go so far as to say javascript objects ARE associative arrays, you can insert, delete and lookup. And you can iterate through the keys. – falstro Dec 20 '10 at 16:02
  • 1
    @roe, No it's not. http://books.google.se/books?id=PXa2bby0oQ0C&pg=PA58&lpg=PA58&dq=Douglas+crockford+on+Arrays&source=bl&ots=HHujt7p0jL&sig=IrXN0mCHvaC7vzEGC5zM8sySMog&hl=sv&ei=VX8PTabcN4aZOsq89L0J&sa=X&oi=book_result&ct=result&resnum=5&ved=0CD8Q6AEwBA#v=onepage&q=Douglas%20crockford%20on%20Arrays&f=false – Anders Dec 20 '10 at 16:09
  • 3
    @Anders; walks like a duck and talks like a duck, I'd call that a duck. Yes, they're called 'object properties' in Javascript, but that doesn't stop them from being an associative array. The link you pasted just says they're not linear arrays, and they're certainly not. If you want to argue that they're not associative arrays, name one property of associative arrays which javascript objects do not possess. – falstro Dec 20 '10 at 16:15
0

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.

Justin
  • 2,559
  • 3
  • 27
  • 33