Get the first key name of a JavaScript object [duplicate]

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.


In Javascript you can do the following:

Object.keys(ahash)[0];

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.

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"