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

95
Visualizações
Rearrange array with new order using javascript

I have an array

const arr = [
   {label: 'a', width: 200},
   {label: 'b', width: 200},
   {label: 'c', width: 200},
   {label: 'd', width: 200},
   {label: 'e', width: 200}
];

given another array

const data = ['d', 'e', 'a', 'c', 'b'];

It need's to re-arrange first array based on the new data.

What function should I use in javascript?

Edit: Thank you for very interesting comments. But to make it more complicated, let's assume that data could also be not the full list of the first array.

const data = ['a', 'c'];

and it still should outcome the first array where first two elements are a & c, and remaining ones are b, d, e. Finished array should be in a list of a, c, b, d, e.

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

0

const arr = [
  { label: "a", width: 200 },
  { label: "b", width: 200 },
  { label: "c", width: 200 },
  { label: "d", width: 200 },
  { label: "e", width: 200 }
];

const data = ["d", "e", "a", "c", "b"];


const updatedArray = data.map((item) => arr.find((t) => t.label === item));
console.log(updatedArray);

about 4 years ago · Juan Pablo Isaza Relatório

0

  • Using Array#reduce, iterate over data to save element-index in a Map
  • Using Array#sort, sort arr by the value of each element in the above Map

const sort = (arr = [], data = []) => {
  const indicesMap = data.reduce((map, e, i) => map.set(e, i), new Map);
  return [...arr].sort(({ label: a}, { label: b }) => {
    const indexA = indicesMap.get(a), indexB = indicesMap.get(b);
    return (indexA === undefined || indexB === undefined) 
      ? isNaN(indexA) - isNaN(indexB)
      : indexA - indexB;
  });
}

const arr = [ {label: 'a', width: 200}, {label: 'b', width: 200}, {label: 'c', width: 200}, {label: 'd', width: 200}, {label: 'e', width: 200} ];
console.log( sort(arr, ['d', 'e']) );
console.log( sort(arr, ['a', 'd']) );
console.log( sort(arr, ['d', 'e', 'a', 'c', 'b']) );

about 4 years ago · Juan Pablo Isaza Relatório

0

simply :

const arr = 
  [ { label: 'a', width: 200} 
  , { label: 'b', width: 200} 
  , { label: 'c', width: 200} 
  , { label: 'd', width: 200} 
  , { label: 'e', width: 200} 
  ] 
const data = ['d', 'e', 'a', 'c', 'b']

arr.sort((a, b) => data.indexOf(a.label) - data.indexOf(b.label))

console.log(arr)

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