1

What's wrong with this code?

let matrix1 = [
        [2, 7, 9, 2],
        [8, 0, 7, 1],
        [8, 8, 0, 8]
    ];
    
let arr = []; // or arr = [[]];
    
for (let i = 0; i < matrix1.length; i++) {
        for (let j = 0; j < matrix1[i].length; j++) {
            arr[i][j] = matrix1[i][j];
        }
    }
    
console.log(arr);

Error is:

Cannot set property '0' of undefined This is when I try to assign the value of an element of matrix1 to the new array. for loop works for the single dimensional array.

Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
M P
  • 11
  • 1
  • `[]` is an empty array. `[ [] ]` is an array with one element which is another array. If you want to clone `matrix1` you have to add `matrix1.length` arrays in `arr` – Andreas Jun 19 '21 at 11:31
  • Please don't do it that way. Check the answers in the dupe target (at the top of your question) or [here](https://stackoverflow.com/questions/13756482/create-copy-of-multi-dimensional-array-not-reference-javascript) for a better (universal) way. – Andreas Jun 21 '21 at 14:49
  • Thank you Andreas. You exaplained what I was doing and what needs to be done. So I tried `let arr = [[], [], []];` and it worked. Now is there any way we can put number of arrays in main array on the fly? I mean if we have different input `matrix1` with different lengths, can we put those many empty arrays inside the main array? Edit: I read your answer after posting mine and got the idea. Thank you. – M P Jun 21 '21 at 14:57

3 Answers3

1

try this

   let matrix1 = [
    [2, 7, 9, 2],
    [8, 0, 7, 1],
    [8, 8, 0, 8]
];

let arr = []; // or arr = [[]];

for (let i = 0; i < matrix1.length; i++) {
    for (let j = 0; j < matrix1[i].length; j++) {
        if(!arr[i]) 
            arr[i] = []
        arr[i][j] = matrix1[i][j];
    }
}

if you want copy a 2d array without for loop try this one:

   let matrix1 = [
    [2, 7, 9, 2],
    [8, 0, 7, 1],
    [8, 8, 0, 8]
];
let arr = JSON.parse(JSON.stringify(matrix1))
0

You need to create a new sub array in the outer loop so that arr[i] is an array and not undefined

    let matrix1 = [
    [2, 7, 9, 2],
    [8, 0, 7, 1],
    [8, 8, 0, 8]
];

let arr = []; 

for (let i = 0; i < matrix1.length; i++) {
    // push a new sub array to be populated in next loop
    arr.push([]);

    for (let j = 0; j < matrix1[i].length; j++) {
        arr[i][j] = matrix1[i][j];
    }
}

console.log(arr);
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

The problem is the line arr[i][j] = matrix1[i][j];, since arr[i][j] is undefined at this point.

The correct way of adding elements to an array is using the .push() function:

let matrix1 = [
  [2, 7, 9, 2],
  [8, 0, 7, 1],
  [8, 8, 0, 8]
];

let arr = [];

for (let i = 0; i < matrix1.length; i++) {

  arr.push([]);
  
  for (let j = 0; j < matrix1[i].length; j++) {
    arr[i].push(matrix1[i][j]);
  }
}

console.log(arr);

Also note that using JSON might achieve the same task in a simpler way:

let matrix1 = [
  [2, 7, 9, 2],
  [8, 0, 7, 1],
  [8, 8, 0, 8]
];

let arr = JSON.parse(JSON.stringify(matrix1))

console.log(arr)
Run_Script
  • 2,487
  • 2
  • 15
  • 30