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.

3
  • [] 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 Commented Jun 19, 2021 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 for a better (universal) way. Commented Jun 21, 2021 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. Commented Jun 21, 2021 at 14:57

3 Answers 3

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))

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.