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

81
Visualizações
Array.some() function did not matched for undefined
var array = [3,4,5];
delete array[1];
console.log(array);
console.log(array[1]);
array.some(element => {
    console.log(element);
    return element === 3
})
array.some(element => {
    console.log(element);
    return element === undefined
})

PS: In the image below array for above code is taken as a and element is taken as b

enter image description here

Why does Array.some() function did not matched for undefined at index 1? Does it handles undefined differently?

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

0

The .some doesn't check the empty index The undefined must be statically written

const myArr = [1,,3];
console.log(myArr)

myArr.some(elem => console.log(elem))

 myArr[1] = undefined;
console.log(myArr)

myArr.some(elem => console.log(elem))

If you want to detect empty indices

this should work

const myArr = [1,,3];


function hadEmptyIndex(arr){
  
  for(const member of arr){
    if(typeof member === "undefined") return true
  }
  
  return false
}


console.log(hadEmptyIndex(myArr))

about 4 years ago · Santiago Trujillo Relatório

0

That's because you are not iterating over empty values, so you can't really check if the value is equal to undefined or not.

Once you delete an item from an array, the array methods such as some, forEach, and etc will skip the item in the iteration.

What you can do instead is setting the element to null, then it's gonna be iterable:

var a = [1, 2, 3];
a[0] = null // [ null, 2, 3 ]
a.some(b => b === null) // true
about 4 years ago · Santiago Trujillo Relatório

0

Great question, array.some() function only iterate over indexes with assign values,

callbackFn is invoked only for indexes of the array with assigned values. It is not invoked for indexes which have been deleted or which have never been assigned values.

for further reading Read Here

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