commandos
commandos

Reputation: 171

JS object access

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) :

enter image description here

Upvotes: 1

Views: 89

Answers (1)

Bergi
Bergi

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

Related Questions