ibrahim mahrir
ibrahim mahrir

Reputation: 31692

How to get the first key of an object?

I have an object and I want to access the first element of that object, so first, I need to get the first key of that object. I have two ways to do it but I'm looking for a better way.

The first is:

var firstKey = Object.keys(myObject)[0];

but Object.keys will return an array of all the keys. So if I want just the first key, this method will be too expensive for my quest (especially if myObject have too many keys).

The second method is:

var firstKey = null;
for(var key in myObject){
    firstKey = key;
    break;
}

this works just fine and it's better than the first one but I think it's hideous (for, break and all that mess just to get the first key).

So

I was wondering if there is a much elegant way to get the first key? Something like:

var firstKey from myObject;

EDIT:

I don't care about the ordering I just want to get whatever the first key is.

EDIT2:

I have an object that holds some informations (lets say, for example, a person's name is a key, his email will be the value). Let's say I have a placeholder where some name and it's email adress are printed. If I press delete, I delete that entry (delete myObject[name];), then I need to fill that placeholder with another name and email (I don't care who anyone will do), so the first (or whatever the first is now) will do.

Upvotes: 2

Views: 8792

Answers (2)

Oriol
Oriol

Reputation: 288310

The only approach which won't collect all properties in an array is the for...in one.

You can always move it to a function for better abstraction:

function firstKey(obj) {
  for (var key in obj)
    if (Object.getOwnPropertyDescriptor(obj, key))
      return key;
}
firstKey({a: 1}); // "a"
firstKey({});     // undefined

However, note that it won't be well defined, because for...in and Object.keys use an implementation-dependent order.

It would be better if [[OwnPropertyKeys]] returned an iterator and there was some way to expose it:

Object.getOwnPropertyNamesIterator(obj).next().value; // this does not exist :(

Upvotes: 2

trincot
trincot

Reputation: 350365

Quoted from MDN's documentation on for...in (I highlight):

The for...in statement iterates over the enumerable properties of an object, in arbitrary order.

And the documentation on Object.keys has:

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

So the concept of first key is a fuzzy one. A lot has already been said about property order in JavaScript objects. You might want to go through Does JavaScript Guarantee Object Property Order?

You could consider to use a Map instead of a plain object with (apparently) dynamic properties. A Map also stores key/value pairs, but guarantees that insertion order is maintained.

Here is an example on how you could set some key/value pairs, and then get the first one. Then it deletes the first, and the next one becomes the first:

let mp = new Map().set('x', 1)
                  .set('y', 3)
                  .set('z', 14);

let key = mp.keys().next().value;
console.log('before delete, the first key is: ', key);

mp.delete(key);

key = mp.keys().next().value;
console.log('after delete, the first key is: ', key);

Although the call to keys() may give the impression that it will first generate a list of all keys, this is not true: it returns an iterator, not an array, and only the call to next will actually fetch one.

Upvotes: 4

Related Questions