293

Let's assume we have the following JavaScript object:

ahash = {"one": [1,2,3], "two": [4,5,6]}

Is there a function that returns the first key name for the given object?

From the example above I want to get one as a result of that function.

3
  • Do you want the value of the first key or the first key name of the object? I'm confused. Commented Jun 12, 2019 at 16:32
  • 7
    let [first] = Object.keys(ahash) Commented Apr 27, 2020 at 8:27
  • 1
    That question is not a duplicate for the another mentioned question, duplicate question is about "How to get the first property value", this question is about "How to get the first property key name". Commented Jul 22, 2023 at 19:19

8 Answers 8

653

In Javascript you can do the following:

Object.keys(ahash)[0];
Sign up to request clarification or add additional context in comments.

7 Comments

Works on browsers as well
this won't work on older browsers which do not support JS 1.8.5
I'd like to point out that for (key in dict) is likely to execute significantly faster, O(1) rather than O(n). It's not silly at all.
to future me... note that there is also a "value" method on Object, i.e. Object.value, if you instead want the value of the first key
@engineerDave, this is future you. We've changed our stackOverflow account handle and we've made a whole bunch of money by inventing time travel, but that is beside the point. I just wanted to say, "Thanks", this little note helped me out.
|
70

You can query the content of an object, per its array position.
For instance:

 let obj = {plainKey: 'plain value'};

 let firstKey = Object.keys(obj)[0]; // "plainKey"
 let firstValue = Object.values(obj)[0]; // "plain value"

 /* or */

 let [key, value] = Object.entries(obj)[0]; // ["plainKey", "plain value"]

 console.log(key); // "plainKey"
 console.log(value); // "plain value"

3 Comments

It does not seem to answer the question
An example using an explicitly constructed object such as the question used would provide a better answer. This answer is basically correct, but it requires too subtle a knowledge of Javascript to easily decipher the meaning.
Thanks for the remarks, changed to more understandable code.
39

There's no such thing as the "first" key in a hash (Javascript calls them objects). They are fundamentally unordered. Do you mean just choose any single key:

for (var k in ahash) {
    break
}

// k is a key in ahash.

6 Comments

should check if obj.hasOwnProperty(prop) also I would add
and how to get the rest of other ?
I think it is not recommended solution. This uses bad feature named 'variable hoisting'. And ES6 has recommended not to use 'var' in for loop
The EMCA script specification makes dictionaries ordered. They are "ordered dictionaries". The order of enumeration is the same as the order in which the dictionary items are added. This is useful. in Python, for example, OrderedDictionary is a specific class, where Dictionary will returned indeterminate ordering of keys in dictionaries on enumeration
so javascript objects are quite complex things, that implement: a key value store; a separate numbered value store (array); prototyping. the key value store part is a "dictionary". * Enumerating keys (e.g. Object.keys(someRandomObject) returns the keys not an arbitrary order, not a random order and not some other specific ordering, but in the same order as they were added. * in Python, your answer would be correct, as there is no "first" key. (i would guess that the "ordering" of keys returned in python has something to do with the key hash)
|
21

Try this:

for (var firstKey in ahash) break;

alert(firstKey);  // 'one'

2 Comments

Note that you must use the var keyword; let or const will not suffice.
No. Please don't do this. This might be correct but it is a completely hacky and confusing solution.
18

If you decide to use Underscore.js you better do

_.values(ahash)[0]

to get value, or

_.keys(ahash)[0]

to get key.

Comments

7

With Underscore.js, you could do

_.find( {"one": [1,2,3], "two": [4,5,6]} )

It will return [1,2,3]

2 Comments

Also true for lodash :)
there's also a findKey method which returns the key (instead of the value)
7

I use Lodash for defensive coding reasons.

In particular, there are cases where I do not know if there will or will not be any properties in the object I'm trying to get the key for.

A "fully defensive" approach with Lodash would use both keys as well as get:

const firstKey = _.get(_.keys(ahash), 0);

Comments

1

you can put your elements into an array and hash at the same time.

var value = [1,2,3];
ahash = {"one": value};
array.push(value);

array can be used to get values by their order and hash could be used to get values by their key. just be be carryfull when you remove and add elements.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.