Reputation: 171
I have a js object and i'm trying to access it directly without having to do something like :
for(i in data) { obj = data[i] }
is there a better way to access this object without looping ? (i'll always have 1 result)
here is the firebug result for console.log(data) :
Upvotes: 1
Views: 89
Reputation: 664185
No, you can't access a property without knowing its name (aside from using fancy for-of-loops). And to get that name, you only can enumerate the properties with a for-in-loop or use Object.keys
/….getOwnPropertyNames
.
If you know that you always have exactly one key in your object, you might have chosen the wrong data structure.
Upvotes: 2