js copy 2d array

Solutions on MaxInterview for js copy 2d array by the best coders in the world

showing results for - "js copy 2d array"
Davide
19 Jan 2019
1let matrix = [
2  [0,0,0,0,0],
3  [0,0,0,0,0],
4  [0,0,0,0,0],
5  [0,0,0,0,0],
6]
7// copies just values, not references!
8function getCopyOfMatrix(mat) {
9  return mat.map(row => row.map(col => col))
10}
11
12let copyOfMatrix = getCopyOfMatrix(matrix); 
Iris
14 May 2018
1let matrix = [
2  [0,0,0,0,0],
3  [0,0,0,0,0],
4  [0,0,0,0,0],
5  [0,0,0,0,0],
6]
7// copies just values, not references!
8function getCopyOfMatrix(mat) {
9  return JSON.parse(JSON.stringify(mat))
10}
11
12let copyOfMatrix = getCopyOfMatrix(matrix);