3

Is there a way to store the result of the 'for loop' in a variable?

I want to display the two name kevin and elsa in a div for example.

I know i can do john.friends[0].nom, john.friends[1].nom;

but if John has many friends, it will be difficult...

Now the variable friendName gives me just name, I understand why but can't see the solution...

Thanks !

function Person(name, age, friends) {
    this.name = name;
    this.age = age;
    this.friends = friends;

};

var john = new Person('john', 28, [new Person('elsa', 29, []), new Person('kevin', 31, [])]);

for (var i = 0; i < john.friends.length; i++) {
    var friendName = john.friends[i].name;
}

alert(friendName);
1
  • I'd ask what exactly are you trying to accomplish? Since you can get john's friends by calling john.friends, just having another variable pointing to those friends is redundant. Commented Jul 12, 2012 at 15:42

4 Answers 4

6
var friendNames = []; // store their names within a local array

for(var i = 0; i < john.friends.length; i++){
    friendNames.push(john.friends[i].name);
}

console.log(friendNames); // log to the browser (readability) instead of alert
Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I just answered, down to the variable names and log call. The question is... by coincidence or design?
2
var friendNames = []; // create array to hold friend names

for(var i=0; i < john.friends.length; i++){    
    friendNames.push(john.friends[i].name); // add friend's name to the array
}

console.log(friendNames); // an array containing all of John's friends' names.

Comments

1

You probably want an array of names (list of all the names). Declare friendName = new Array() and friendName.push(john.friends[i].name);

var friendName = new Array();
for (var i = 0; i < john.friends.length; i++) {
    friendName.push(john.friends[i].name);
    //or friendName[i] = john.friends[i].name;
}
//friendName is now an array of names. Use it as you need (the last line is useful for debugging)
for (i = 0; i < friendName.length; i++) alert(friendName[i]);

The problem with your old code was that each iteration of the loop would reassign friendName. When i = 0, 'elsa' was assigned to friendName. When i = 1, 'kevin' was reassigned to friendName.

Furthermore, javascript does not have block scoping; it would be like calling var friendName = 'elsa'; var friendName = 'kevin';. Won't crap out the javascript engine, but just watch out for that. Better to declare friendName as an array above the loop as others and I have mentioned.

Comments

1

Since your friends variable is an array, you can just use some of the methods available to you on that type:

var friendString = john.friends.map(function(f) { return f.name; }).join(' ');

In the above example, the map method maps the friends to just their names and then the join method will produce a space separated string of all johns friends.

2 Comments

Mmmmm i don't understand the map method return me an array like this: ["",""]
@user1521149 Answer updated - I didn't include the argument in the callback. Apologies.

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.