Home > Software engineering >  Cannot read properties of undefined (reading 'length') in javascript
Cannot read properties of undefined (reading 'length') in javascript

Time:05-28

function printMyArr(arr){
    for(var i = 0; i < arr.length; i  ) {
          for(var j = 0;j <arr[0].length;j  ){
            console.log(arr[i][j])}}}

var arr = [[1, 2], [3, 4],[5, 6]]; printMyArr();

CodePudding user response:

You need to pass in the array to printMyArr() like this printMyArr(arr).

When you get an error like this "Cannot read properties of undefined (reading 'length')" it means that wherever you're trying to access .length there's nothing there. In this case it's not there because you didn't pass it in.

CodePudding user response:

Try simple solution using forEach()

OuterArry.forEach((arrItem) => {
    arrItem.forEach((item) => {
        console.log(item);
    });
});

  • Related