<p>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):</p>

<pre><code>getKey = function (data) {
    var firstKey;
    $.each(data, function (key, val) {
        firstKey = key;
        return false;
    });
    return firstKey;
};
</code></pre>

<p>Just guessing, but I'd say there's got to be a better (read: more efficient) way of doing this. Any suggestions?</p>

<p>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.</p>

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;
}
```
```