A for in loop is used to enumerate objects. You are trying to iterate over an array. A better technique for iterating would be a regular for loop or the Array.forEach()
method.
In a for in loop, the variable contains the key, not the value. To get the value, use the key:
for(var i in friends){
var friend = friends[i];
...
}
But, again, this enumerates an object, including any properties besides the array elements. This means that if any code adds a method to Array.prototype
, that method will also be enumerated. Using .forEach()
is better:
friends.forEach(function (friend, i) {
...
});
Better yet, since you are trying to create a new Array based on the contents of another, use Array.map()
. That is exactly what it's for.
var rooms = friends.map(function (friend, i) {
return 'userid'+friend+'friends';
});