Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Assigning a value to a JavaScript matrix inside of a forEach gone wrong

The bitGraph.dfs() function returns an array of strings, with a format of 'i,j'. The matrix ends up with columns of ones and zeros. The goal is to have the indexes parsed from the strings of the matrix assigned to ones.

I checked the debugger and the line result[x][y] = 1 doesn't execute with each forEach iteration, but instead assigns a 1 to each element of the y column of the matrix at the end of the forEach.

const result = Array(N).fill(Array(M).fill(0));

Inside of a for loop:

bitGraph.dfs(`${N - 1},${j}`, visited)
    .forEach(node => {
        const [x, y] = node.split(',')
            .map(index => Number(index));
        result[x][y] = 1;
    });

Example:

returned array: ['5,0', '4,0', '1,5', '1,4', '1,3']
result: [
    [ 1, 0, 0, 1, 1, 1 ],
    [ 1, 0, 0, 1, 1, 1 ],
    [ 1, 0, 0, 1, 1, 1 ],
    [ 1, 0, 0, 1, 1, 1 ],
    [ 1, 0, 0, 1, 1, 1 ],
    [ 1, 0, 0, 1, 1, 1 ]
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your

const result = Array(N).fill(Array(M).fill(0));

creates an array filled with N copies of a single Array(M).fill(0) array.

Thus modifying any of those N copies seems to modify all of them:

> const result = Array(3).fill(Array(4).fill("hello"));
(3) [Array(4), Array(4), Array(4)]
  0: (4) ['hello', 'hello', 'hello', 'hello']
  1: (4) ['hello', 'hello', 'hello', 'hello']
  2: (4) ['hello', 'hello', 'hello', 'hello']
> result[0] === result[1]
true  # same object!
> result[0][2] = "zoop"
'zoop'
> result
(3) [Array(4), Array(4), Array(4)]
  0: (4) ['hello', 'hello', 'zoop', 'hello']
  1: (4) ['hello', 'hello', 'zoop', 'hello']
  2: (4) ['hello', 'hello', 'zoop', 'hello']

You'll need something like

const result = Array(N).fill(0).map(_ => Array(M).fill(0));

i.e. fill the array with N zeroes, then replace them with unique new M-length arrays. (You can't just use .map without the .fill, since the array is full of empty objects that aren't mapped over.)

Voilà:

> const result = Array(3).fill(0).map(_ => Array(4).fill("hello"));
> result[0] === result[1]
false
> result[0][2] = "zoop"
'zoop'
> result
(3) [Array(4), Array(4), Array(4)]
  0: (4) ['hello', 'hello', 'zoop', 'hello']
  1: (4) ['hello', 'hello', 'hello', 'hello']
  2: (4) ['hello', 'hello', 'hello', 'hello']
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda