views:

183

answers:

5

var foo = { "bar": {"blah": 9 } };

Is there a way to get the ["blah"] value of the only member of foo if I don't know the key is "bar"?

Can I somehow reference the first member of an object without knowing its key? I'm looking for the equivalent of foo[0]["blah"] if foo were a normal array.

In my case, I can't practically iterate on foo to get the key.

Is that clear?

+4  A: 

As far as I know, with a Javascript Object (in the literal sense, the object of type `Object) the only way to do this is:

for(var i in foo) {
    var value = foo[i].blah;
    break;   
}

Now value will contain the value of the first enumerable property of the bar object in the foo object. You could, of course, abstract this into a function. I was going to write an example, but CMS has a fantastic one in his answer here.

jason
the only problem here is that your used "bar" in your code. The author wants to be able to "explore" the object's internals without prior knowledge of its structure
DmitryK
A: 

I'm afraid what you're attempting to do is just not possible. Objects are essentially hash maps, so the order in which properties are stored should appear at a random location in the object's memory (And the better the hash function, the more random the locations).

You can see this for yourself if you step through a loop that iterates over all the properties of an object. There is no set order in which to iterate through because of the random nature of the structure.

Perhaps you could store the values in both an object and an array?

CookieOfFortune
+1  A: 

Objects in Javascript are unordered collection of name/value pairs, so there's really no such thing as accessing first or last property of an object. The only way to find certain key's value is to iterate over an object (with for-in). You can stop on the first iteration, but an order is not specified, so two different implementations can return two different keys on a first iteration.

kangax
right, I understand that, but in this case there's only one member so order doesn't matter. but if there's no way to specifically reference it, I'll have to iterate. Thanks
brad
A: 

Just have a look here:

http://dean.edwards.name/weblog/2006/07/enum/

how they iterate through the object without knowing its structure.

DmitryK
+2  A: 

Edit: Agree with @kangax, it's much more safe to make a normal function, without polluting the native Object.prototype, which can lead to unexpected behaviors:

function firstMember (obj) {
  for(var i in obj) 
      if (obj.hasOwnProperty(i)){ // exclude properties from the prototype
          return obj[i];
      }
}


firstMember(foo)['blah']; // 9
CMS
Why pollute `Object.prototype`?
kangax
that's what I was looking for. thanks. I might be able to do it on my class instead of Object.prototype.
brad
There's nothing wrong with adding to the object's prototype. That's the point and beauty of prototypical OOP.
jason
@jason There are things that are best left untouched. `Object.prototype` is one of them. Doing so does not make prototypical nature of Javascript any less beautiful.
kangax
@kangax: edited, completely agree with you, it's really an **overkill** to have **ALL** objects receive this additional member... lack of sleep + coffee...
CMS