“JS Copy 2D Array” Respostas de código

JS Copy 2D Array

let matrix = [
  [0,0,0,0,0],
  [0,0,0,0,0],
  [0,0,0,0,0],
  [0,0,0,0,0],
]
// copies just values, not references!
function getCopyOfMatrix(mat) {
  return mat.map(row => row.map(col => col))
}

let copyOfMatrix = getCopyOfMatrix(matrix); 
Ill Ibex

JS Copy 2D Array

let matrix = [
  [0,0,0,0,0],
  [0,0,0,0,0],
  [0,0,0,0,0],
  [0,0,0,0,0],
]
// copies just values, not references!
function getCopyOfMatrix(mat) {
  return JSON.parse(JSON.stringify(mat))
}

let copyOfMatrix = getCopyOfMatrix(matrix); 
Ill Ibex

JavaScript cópia 2D Array

function copy2DArray(array) {
    return array.map(row => [...row]);
}
anderium

Respostas semelhantes a “JS Copy 2D Array”

Perguntas semelhantes a “JS Copy 2D Array”

Mais respostas relacionadas para “JS Copy 2D Array” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código