const listOfNeighbours = [
["Canada", "Mexico"],
["Spain"],
["Norway", "Sweden", "Russia"],
];
for (let i = 0; i < listOfNeighbours.length; i++) {
for (let j = 0; j < listOfNeighbours[j].length; j++) {
console.log(`Neighbours : ${listOfNeighbours[i][j]}`);
}
}
Neighbours : Canada
Neighbours : Spain
Neighbours : Norway
In this question only first ones are printed . How to write all of them to console log
I want like this below in order
Neighbours : Canada
Neighbours : Mexico
Neighbours : Spain
Neighbours : Norway
Neighbours : Sweden
Neighbours : Russia
3
Answers
Your inner loop is only going to listOfNeighbours[j], using j, which is the current iterator. Us
i
as the index for listOfNeighbors in the inner loopfor (let j = 0; j < listOfNeighbours[i].length; j++) {you can also use
flat()
, which simplifies the syntax.It’s not with for loop but you will get the same result: