admin管理员组文章数量:1325236
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?
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?
Share Improve this question asked Aug 25, 2009 at 4:10 bradbrad 4432 gold badges5 silver badges11 bronze badges5 Answers
Reset to default 6As 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.
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
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.
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?
Just have a look here:
http://dean.edwards.name/weblog/2006/07/enum/
how they iterate through the object without knowing its structure.
本文标签: javascriptreferencing JS object member without the keyStack Overflow
版权声明:本文标题:javascript - referencing JS object member without the key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742167755a2426144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论