191

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.

Nikita Fedyashev
  • 15,938
  • 11
  • 41
  • 69
Anton Koval'
  • 3,824
  • 4
  • 26
  • 40

8 Answers8

439

In Javascript you can do the following:

Object.keys(ahash)[0];
webmaster
  • 1,658
  • 21
  • 25
Pickels
  • 30,764
  • 23
  • 107
  • 174
37

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.
Ned Batchelder
  • 323,515
  • 67
  • 518
  • 625
  • 7
    should check if obj.hasOwnProperty(prop) also I would add – vsync Dec 14 '12 at 06:40
  • and how to get the rest of other ? – Santosh Jun 03 '17 at 10:21
  • 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 – 刘宇翔 Aug 01 '17 at 03:53
  • 1
    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 – Jay Day Zee Apr 12 '18 at 16:16
  • @JayDayZee ECMAScript doesn't have a thing called "dictionary", right? This answer (https://stackoverflow.com/a/5525820/14343) seems to indicate that objects are unordered, and Maps are ordered. Does that mesh with your understanding? – Ned Batchelder Apr 12 '18 at 23:28
  • 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) – Jay Day Zee Apr 14 '18 at 11:57
26

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"
Ilya Degtyarenko
  • 782
  • 6
  • 13
  • 1
    It does not seem to answer the question – xiawi Jul 10 '19 at 11:57
  • 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. – CXJ Jul 10 '19 at 21:19
  • Thanks for the remarks, changed to more understandable code. – Ilya Degtyarenko Jul 11 '19 at 12:22
18

If you decide to use Underscore.js you better do

_.values(ahash)[0]

to get value, or

_.keys(ahash)[0]

to get key.

Bunyk
  • 6,415
  • 4
  • 36
  • 68
15

Try this:

for (var firstKey in ahash) break;

alert(firstKey);  // 'one'
nickf
  • 499,078
  • 194
  • 614
  • 709
8

With Underscore.js, you could do

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

It will return [1,2,3]

mjlescano
  • 759
  • 8
  • 13
3

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);
random_user_name
  • 23,924
  • 7
  • 69
  • 103
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.

yilmazhuseyin
  • 5,592
  • 3
  • 29
  • 38