1

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

4
  • 1
    Are you looking for data[8][8]? what a key name! Commented Jan 17, 2013 at 16:57
  • Are you aware of the key name that you want to access before trying to loop? Commented Jan 17, 2013 at 16:58
  • no i dont have access to the key name ... this is the user id that i'm returning . Commented Jan 17, 2013 at 17:00
  • 1
    See this answer. Object.keys[0] would do it for all browsers, except IE8 and below. Commented Jan 17, 2013 at 17:00

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

in that specific case yes i get only 1 result because i'm using the same function that return all users .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.