Help--- > Nesting For Loops

cannot understand the inner loop

var arr = [
  [1,2], [3,4], [5,6]
];
for (var i=0; i < arr.length; i++) {
  for (var j=0; j < arr[i].length; j++) {
    console.log(arr[i][j]);
  }
}

You have an array inside an array.

var arr = [ 1, 2, 3 ];  //This is single dimension array
var arr = [ [1,2], [3,4], [5,6] ] // This is multi dimensional array

If we think of them in terms of index. Your first loop is going through:

index  i      0      1      2
var arr = [ [1,2], [3,4], [5,6] ]

Since your values are all arrays as well, we use second loop to iterate through its value

    i
arr[0] = [1,2]  //This is your first loop;
arr[1] = [3,4]
arr[2] = [5,6]

    i  j
arr[0][0] = [1]  //This is your second loop;
arr[0][1] = [2]

arr[1][0] = [3]
arr[1][1] = [4]

arr[2][0] = [5]
arr[2][1] = [6]
3 Likes

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.