What is the most efficient way to get the first item from an associative array in JavaScript?
Solution 1
There isn't really a first or last element in associative arrays (i.e. objects). The only order you can hope to acquire is the order the elements were saved by the parser -- and no guarantees for consistency with that.
But, if you want the first to come up, the classic manner might actually be a bit easier:
function getKey(data) {
for (var prop in data)
return prop;
}
Want to avoid inheritance properties?
function getKey(data) {
for (var prop in data)
if (data.propertyIsEnumerable(prop))
return prop;
}
Solution 2
You can avoid having to create a function by referencing the first entry returned from Object.keys()
:
var firstKey = Object.keys(data)[0];
For the first entry from a sorted key list, simply add a call to the .sort()
method:
var firstKey = Object.keys(data).sort()[0];
Solution 3
In addition to Jonathan's solution, we can also extend the default array functionality:
Array.prototype.getKey = function() {
for (var prop in this)
if (this.propertyIsEnumerable(prop))
return prop;
}
Related videos on Youtube
Harshit Shah
Professional web developer since January 1, 1998.
Updated on December 24, 2020Comments
-
Harshit Shah over 3 years
I need to get just the first item (actually, just the first key) off a rather large associative array in JavaScript. Here's how I'm doing it currently (using jQuery):
getKey = function (data) { var firstKey; $.each(data, function (key, val) { firstKey = key; return false; }); return firstKey; };
Just guessing, but I'd say there's got to be a better (read: more efficient) way of doing this. Any suggestions?
UPDATE: Thanks for the insightful answers and comments! I had forgotten my JavaScript 101, wherein the spec says you're not guaranteed a particular order in an associative array. It's interesting, though, that most browsers do implement it that way. I'd prefer not to sort the array before getting that first key, but it may be unavoidable given my use case.
-
John Millikin over 15 yearsIf it's an associative array, how do you know what the first item is?
-
ethan over 15 yearsI think he means the first item that was inserted into the array.
-
SarahK over 15 yearsIf you want that, you would need to build your own implementation of a Stack. Assoc arrays don't preserve any key ordering.
-
olliej over 15 yearsThey do in JS -- go backwards compatibility! :D
-
-
olliej over 15 yearsActually you are guaranteed order -- not by the spec, but simply by virtue of too many sites depending on order in the past. Basically you are guaranteed that for (a in b) .. will cover each enumerable property in b in the exact order they were added to the object at runtime.
-
olliej over 15 years(I ran out of space above) The exception to this is the order the you enumerate properties in the prototype chain which still varies from browser to browser
-
Riad over 9 yearsthis is the best one i think... +1 !
-
Jonathan Lonowski over 9 years@CyprianGuerra It should work fine for "own" properties and Erik has already added an answer with it. This Q&A was just posted over a year before
Object.keys()
was formally specified (EMCA 262 5th was released in Dec 2009). -
cprn over 9 years@JonathanLonowski My fault, sorry, missed it somehow. Thanks for pointing that out.
-
thdoan about 9 yearsIf you need support for older browsers, then be sure to include this polyfill in your script: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…