1

I need to get name of each object which is inside of another object. this should be done in for loop like:

for (var obj1 in obj2){ // use obj1.getName } 

if this is not possible (but it would be better if yes), I can use something like name property for each object in obj2. this property will store object name.

basically I dont know why obj1 is undefined when program goes through for loop.

EDIT: problem with undefined was in firebug breakpoint on for header row. it missed some code and show me undefined

5
  • Can you post a jsfiddle with the declaration of your object? Or maybe post it inside the question Commented May 29, 2013 at 12:55
  • 1
    May be you have missed something Commented May 29, 2013 at 12:57
  • If obj1 really is undefined than that's would be a really weird behaviour since the for loop is only looping through properties that actually exist. Or is it that just obj1.getName is undefined? Commented May 29, 2013 at 13:23
  • @koubin your solution is as weird as your problem :) Commented May 29, 2013 at 13:29
  • @basilikum it was another problem. I commented it in edited question, sorry :-) Commented May 29, 2013 at 13:38

2 Answers 2

1

When iterating in a for..in loop like that, you will see obj1 is assigned to each key in sequence, which means you can get the value as obj2[obj1]. That's all there is to it, it couldn't be simpler.

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

Comments

1

When you want to select a dynamic key in an object you use the array notation like so:

for (var obj1 in obj2){ obj2[obj1] } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.