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

228
Visualizações
return reordered version of array b based on reordered version of a

I have two arrays a and b like this:

const a = [1,2,3,4]
const b = ['1','2','3','4'] // could be 'a','b','c','d'

const reordered_a = [4,1,3,2] // based on this reordering 

function reorder_b() {
    // should return ['4','1','3','2']
} 

How can I return reordered version of b based on reordered positions in reordered_a?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could take an object with the indices for the values and map the pattern with the values of the indices.

const a = [1, 2, 3, 4]
const b = ['1', '2', '3', '4'] // could be 'a','b','c','d'

const reordered_a = [4, 1, 3, 2] // based on this reordering 

function reorder_b() {
   const references = Object.fromEntries(Object.entries(a).map(a => a.reverse()));
   return reordered_a.map(k => b[references[k]]);
}

console.log(reorder_b()); // 4 1 3 2

about 4 years ago · Juan Pablo Isaza Relatório

0

What you got to do is first use every pair of elements el1 and el2 and convert them to numbers. Then once they're converted, make sure that the latter is bigger than the latter by checking their difference :

return b.sort((el1,el2)=>Number(el2)-Number(el1))
about 4 years ago · Juan Pablo Isaza 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