this code is only iterating the first 9 nos in the nested array.
var arr = [[[1,2,3,4,5,6,7,8,9],[1,2,3,4]]];
for (var i=0; i < arr.length; i++) {
for(var j = 0; j < arr[i].length; j++) {
for(var k = 0; k < arr[j].length; k++){
console.log(arr[i][j][k]);
};
};
};
Ala Eddine Menai
2,9047 gold badges32 silver badges61 bronze badges
3 Answers 3
The Problem is with the third loop. Corrected Code -
for (var i=0; i < arr.length; i++) {
for(var j = 0; j < arr[i].length; j++) {
for(var k = 0; k < arr[i][j].length; k++){
console.log(arr[i][j][k]);
};
};
};
answered Jun 24, 2020 at 18:43
Gauri Shankar Gupta
1395 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You had missed an iterator on your second loop Here
for(var i=0;i<arr.length;i++){
for( var j=0;j<arr[i].length;j++){
for (var k=0;k<arr[i][j].length;k++)
console.log(arr[i][j][k]);
}
}
Comments
You can also use Foreach as an alternative way to loop your array.
const iterate = (arr) =>
arr.forEach((arrOne) =>
arrOne.forEach((arrTwo) => arrTwo.forEach((value) => console.log(value)))
);
answered Jun 24, 2020 at 18:58
Ala Eddine Menai
2,9047 gold badges32 silver badges61 bronze badges
Comments
lang-js
arr[i][j].lengthfor-loopfor you ?