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

124
Visualizações
unexpected result when mapping two arrays with undefined values

Why does this map return different results when I apply it on two arrays that are supposed to be perfectly equal to each other?

let a = [1, 2, 3];
a.length = 7;
let b = [1, 2, 3, undefined, undefined, undefined, undefined];
console.log("a: " + a); //a: 1,2,3,,,,
console.log("b: " + b); //b: 1,2,3,,,,
console.log("It is " + a.every((x, i) => x === b[i]) + " that these arrays are perfectly equal."); //It is true that these arrays are perfectly equal.
a = a.map(x => 1);
b = b.map(x => 1);
console.log("a: " + a); //a: 1,1,1,,,,
console.log("b: " + b); //b: 1,1,1,1,1,1,1

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Array a has 4-7 just as empty. In b 4-7 are undefined. After using the map function, that difference is showing up as "1,1,1,,,," and "1,1,1,1,1,1,1"

about 4 years ago · Santiago Trujillo Relatório

0

These two arrays are not the same. pic

Due to this doc when you change the length of an array javascript injects "empty statement" in new indexes.

about 4 years ago · Santiago Trujillo 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