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

200
Vistas
Is there a reason why multiple array elements are changing simultaneously after updating just one?

While attempting to make a change to a single array element by adding a string to it, it then updates the rest of the array elements with the same string value. Instead of changing a single element, it has updated the rest of the array with the same value.

I have tried to find an explanation of why more than one array element is changing without a loop, but it does not look possible without .apply, ..., or a loop:

let arrayFiller = Array(20).fill([]);
let myArray= ['foo', 'foo', 'foo'];
[].push.apply(myArray, arrayFiller);
console.log(myArray);
myArray[3][0] = 'bar';
console.log(myArray);
// Outputs: [
  'foo',     'foo',     'foo',
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ]
]

What I expected:

let arrayFiller = Array(20).fill([]);
let myArray= ['foo', 'foo', 'foo'];
[].push.apply(myArray, arrayFiller);
console.log(myArray);
myArray[3][0] = 'bar';
console.log(myArray);
// Outputs: [
  'foo',     'foo',     'foo',
  [ 'bar' ], [], [],
  [], [], [],
  [], [], [],
  [], [], [],
  [], [], [],
  [], [], [],
  [], []
]

Btw, this also works:

let arrayFiller = Array(20).fill([]);
let myArray= ['foo', 'foo', 'foo'];
[].push.apply(myArray, arrayFiller);
console.log(myArray);
myArray[4][0] = 'bar'; // Changed from myArray[3][0] to myArray[4][0]
console.log(myArray);
// Outputs: [
  'foo',     'foo',     'foo',
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ], [ 'bar' ],
  [ 'bar' ], [ 'bar' ]
]
about 4 years ago · Juan Pablo Isaza
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