How to loop through Node.js array
For..in is used to loop through the properties of an object, it looks like you want to loop through an array, which you should use either For Of, forEach or For
for(const val of rooms) {
console.log(val)
}
Using forEach() with your code example (room is an object) would look this:
temp1.rooms.forEach(function(element)
{
console.log(element)
});
Using For of with your code sample (if we wanted to return the rooms) looks like:
for(let val of rooms.room)
{
console.log(val.room);
}
Note: notable difference between For of and forEach, is For of supports breaking and forEach has no way to break for stop looping (without throwing an error).
for (var i in rooms) {
console.log(rooms[i]);
}
Note it's good practice to do a hasOwnProperty
check with in
and it is for objects. So you're better off with for...of
or forEach
.