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

142
Views
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 answers
Answer question

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 Report

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 Report

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 Report
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!