0

Is it possible to retrieve the first attribute name in a javascript object?

Example:

var example = {
    'att1': { /* stuff1 */},
    'att2': { /* stuff2 */},
    'att3': { /* stuff3 */}
};

I'm looking for a function that returns the first attribute name, that is string: att1.

jdabrowski
  • 1,805
  • 14
  • 21

4 Answers4

2

Since objects are unordered, you won't be able to get the "first" one, but you can iterate over the key names and check for specific values.

for (var key in example) {
   if (key === 'att1') {
      var val = example[key];
   }
}
Jack Guy
  • 8,346
  • 8
  • 55
  • 86
  • objects are not un-ordered. they never really were in the field, but now even the spec is clear... – dandavis Mar 10 '16 at 17:19
  • unlike the `Object.keys()`-based answers, this one would hit `toString` and other prototype properties... – dandavis Mar 10 '16 at 17:23
0

In JavaScript, Objects are order-agnostic, so there's no guaranteed way to make sure that the first attribute will be what you want. There's two options for you as I see it...

Option 1: use an array instead

 var example = [
   { /* stuff1 */},
   { /* stuff2 */},
   { /* stuff3 */}
 ];

Now you can safely acces the first object with `example[0].

Option 2: use Object.keys

Object.keys will create an array out of all the keys (attributes, as you put it) in the object. So using your example, it would loke like this:

var example = {
  'att1': { /* stuff1 */},
  'att2': { /* stuff2 */},
  'att3': { /* stuff3 */}
};

// This will produce this array: ['attr1','attr2','attr3']
var keys = Object.keys(example)

So with keys now being an array of all the objects property names, if (and if being the focus here) you know the specific name you're looking for you can just loop over keys until you find it like so:

 keys.map(function(key) {
   if(key === 'attr1') {
     /* do something with example[key] */
   }
 });

Hope this helps!

The Qodesmith
  • 3,205
  • 4
  • 32
  • 45
0

You can get all the property do this

var example = {
 'att1': { /* stuff1 */},
 'att2': { /* stuff2 */},
 'att3': { /* stuff3 */}
};

for (var prop in example) {
  console.log("example." + prop + " = " + example[prop]);
}

// Output:
// "example.att1 = { /* stuff1 */}"
// "example.att2 = { /* stuff2 */}"
// "example.att3 = { /* stuff3 */}"

And you can get the first property do this

var example = {
  'att1': { /* stuff1 */},
  'att2': { /* stuff2 */},
  'att3': { /* stuff3 */}
};

for (var prop in example) {
  if (prop == 'att1') {
    console.log("example." + prop + " = " + example[prop]);
  }     
}

// Output:
// "example.att1 = { /* stuff1 */}"

As a function declaration

var example = {
  'att1': { /* stuff1 */},
  'att2': { /* stuff2 */},
  'att3': { /* stuff3 */}
};

function getFirstProp() {
 for (var prop in example) {
   if (prop == 'att1') {
     return "example." + prop + " = " + example[prop];
   }     
 }
}

console.log(getFirstProp());

// Output:
// "example.att1 = { /* stuff1 */}"
Akinjide
  • 2,723
  • 22
  • 28
-1

You can get the Keys by using Object.keys method.

Object.keys(example)[0]
Prasanna Rkv
  • 419
  • 4
  • 12
  • There are no guarantees that this will be the right key. – Jack Guy Mar 10 '16 at 17:09
  • 1
    @Harangue: when would that code not return "att1" ? – dandavis Mar 10 '16 at 17:17
  • @Harangue @dandavis: The code wouldn't necessarily return "attr1" because there's no guarantee of the order. But you're right in thinking that it will only return the key name, not the property's value. For that you would do something like `example[Object.keys(example)[0]]`. – The Qodesmith Mar 10 '16 at 17:22
  • @TheQodesmith: can you demonstrate a simple example of this re-ordering? – dandavis Mar 10 '16 at 17:26
  • @dandavis See the 2nd answer (by @Alnitak) here: http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order In short, the spec doesn't guarantee the order, but most browsers will in fact preserve it. Better safe than sorry. – The Qodesmith Mar 10 '16 at 17:30
  • @TheQodesmith: yeah, that is outmoded. never really was true in practice, and now it's false on paper too. it's very common mis-concerption though, so you should be forgiven for the mix-up... – dandavis Mar 10 '16 at 17:31