javascript - How to get the first key of an object? - Stack Overflow

I have an object and I want to access the first element of that object, so first, I need to get the fir

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.

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.

Share Improve this question edited Jan 27, 2017 at 21:02 ibrahim mahrir asked Jan 27, 2017 at 20:37 ibrahim mahriribrahim mahrir 31.7k5 gold badges49 silver badges77 bronze badges 17
  • 1 What do you mean by "first element of that object"? – Harris Commented Jan 27, 2017 at 20:39
  • 3 There really is no "first" key in an object. E.g. Object.keys(myObject)[0] will give you the key that was created first in most browsers, but this is not certain. – Tholle Commented Jan 27, 2017 at 20:40
  • 2 Possible duplicate of Access the first property of an object – Tholle Commented Jan 27, 2017 at 20:41
  • 1 @Oriol: The order of Object.keys is implementation dependent prior to ES5. After ES5 it is defined in the spec. I know, stupid to have an unordered collection to have order but that's what people asked for. Currently all browsers and node.js support the specified ordering but transpilers like babel etc. don't support it. – slebetman Commented Jan 27, 2017 at 20:42
  • 1 BTW, even if the ordering of properties in objects are specified you should not depend on it. The specified ordering is: keys that look like integers always e first and are ordered numerically, then all other properties are ordered by insertion order. So, if someone adds a numeric key to your object then you're screwed. – slebetman Commented Jan 27, 2017 at 20:44
 |  Show 12 more ments

2 Answers 2

Reset to default 4

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744146058a4560428.html

相关推荐

  • javascript - How to get the first key of an object? - Stack Overflow

    I have an object and I want to access the first element of that object, so first, I need to get the fir

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信