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

156
Visualizações
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
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