Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
How to change an element in a multidimensional array in Javascript

I've created a mutidimensional array like this: let array = new Array(7).fill(Array(7).fill());

Then I tried to change array[0][0], like this: array[0][0] = 1;

But somehow, this changes the first element of every inner array. This is the output I've got:

Array(7)
0: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
1: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
2: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
3: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
4: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
5: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
6: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]

I can't imagine what I did wrong. Any ideas?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const newArr = (a, b) => {
  return Array.from({length: a}, (_, i) => {
    return Array.from({length: b}, (_, j) => {
      return [i, j]
    })
  })
}
console.log(newArr(4, 3));
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are using the same array reference when filling with the array. Instead use a .map() to fill instead.

const arr2D = Array(7).fill(0).map(()=> Array(7).fill(0));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda