Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

73
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!