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

159
Views
Is there an explanation for why we have to use Array.from and new Set in this situation?

So my task is to merge 2 arrays and return the elements in ascending order. I typed in the following function

function mergeArrays(arr1, arr2) {
 return arr1.concat(arr2).sort((a,b) => (a-b))
}

console.log(mergeArrays([1,3,5,7,9,11,12], [1,2,3,4,5,10,12])) 

Console: expected [ Array(14) ] to deeply equal [ 1, 2, 3, 4, 5, 7, 9, 10, 11, 12 ]

Since the concat method merges the arrays together and returns a new one, and applying the sort method will arrange them in ascending order, why it does not return [ 1, 2, 3, 4, 5, 7, 9, 10, 11, 12 ]?

But using this code

function mergeArrays(arr1, arr2) {
   return Array.from(new Set(arr1.concat(arr2).sort((a,b) => (a-b))))
}

will make it work. I just want an explanation because I dont get why we have to use Array.from and new Set in this situation.

about 4 years ago · Juan Pablo Isaza
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!