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

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

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 Report

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 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!