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

136
Visualizações
Best way to re arrange array of object?

I have an array or object which will serve as columns for a table with a unique key

const list = [{
    key: "Name",
    textColor: "red"
  },{
    key: "Age",
    textColor: "green"
  },{
    key: "Occupation",
    textColor: "yellow"
}]

And, I have a list of ordering of columns in the table

const newOrder = ["Occupation", "Name", "Age"]

Now , how can i rearrange the list according to the newOrder without using nested loops. Also, these all are dyanamic, so its not just about the above mentioned three columns

Expected Output

const list = [{
    key: "Occupation",
    textColor: "yellow"
  },{
    key: "Name",
    textColor: "red"
  },{
    key: "Age",
    textColor: "green"
}]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use just regular sort

const list = [{key: "Name",textColor: "red"},{key: "Age",textColor: "green"},{key: "Occupation",textColor: "yellow"}];
const newOrder = ["Occupation", "Name", "Age"];

const result = list.sort(({key: a}, {key: b}) => newOrder.indexOf(a) - newOrder.indexOf(b));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

about 4 years ago · Juan Pablo Isaza Relatório

0

Your list can be reformatted to a regular javascript object in which key is the property name, and textColor is the value:

const toObject = kvps => Object.fromEntries(kvps.map(kvp => [ kvp.key, kvp.textColor ]));

With a given array of keys, you can pick values from that object like so:

const fromObject = (order, obj) => order.map(key => ({ key, textColor: obj[key] }));

Chain the two together, and you can reorder any list of key value pairs:

const list = [{
    key: "Name",
    textColor: "red"
  },{
    key: "Age",
    textColor: "green"
  },{
    key: "Occupation",
    textColor: "yellow"
}]


const toObject = kvps => Object.fromEntries(kvps.map(kvp => [ kvp.key, kvp.textColor ]));
const fromObject = (order, obj) => order.map(key => ({ key, textColor: obj[key] }));
const reorder = (order, kvps) => fromObject(order, toObject(kvps));


const newList = reorder(["Occupation", "Name", "Age"], list);

console.log(
  newList
)

Edit: if the sizes of your list and order arrays are small, you probably want to go with the much easier to read approach suggested by Jon Webb in one of the other answers. 🙂 I tried to keep my solution to an O(n + m) complexity rather than O(n * m), (n = list size, m = order size) but it's probably not worth the added complexity.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can iterate on the "order" list, finding the corresponding item in the original list, and push the item to the new list in that order:

const orderList = (list, order) => {
  const newList = [];

  for (key of order) {
    const item = list.find((obj) => obj.key == key);
    if (item) newList.push(item);
  }

  return newList;
}
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