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

62
Views
Group and sort objects

I would like to sort an object which you can see below. I want to add all the rating properties for each object and sort the object depending on which rating is highest.

So for example, if Intro 1 total rating is equal to 7 and Intro 2 total rating is equal to 3, then Intro 1 should be the first object.

{
  'Intro 2': [
    {
      deckName: 'Test 2',
      rating: 1
    },
    {
      deckName: 'Test 2',
      rating: 2
    }
  ],
  'Intro 1': [
    {
      deckName: 'Test 1',
      rating: 3
    },
    {
      deckName: 'Test 1',
      rating: 4
    }
  ]
}
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Here's your data

const data = {
  "Intro 2": [
    {
      deckName: "Test 2",
      rating: 1
    },
    {
      deckName: "Test 2",
      rating: 2
    }
  ],
  "Intro 1": [
    {
      deckName: "Test 1",
      rating: 3
    },
    {
      deckName: "Test 1",
      rating: 4
    }
  ]
};

Let's convert it into array and sort through

const dataArr = Object.entries(data);

const sorted = dataArr.sort(
  (a, b) =>
    b[1].reduce((acc, curr) => {
      return acc + curr.rating;
    }, 0) -
    a[1].reduce((acc, curr) => {
      return acc + curr.rating;
    }, 0)
);

And finally converting the array back to object

const obj = {};

sorted.forEach((item) => {
  obj[item[0]] = item[1];
});

Code Sandbox: https://codesandbox.io/s/sleepy-ramanujan-dwzwjm?file=/src/index.js

UPDATE
For the last cycle of converting array to object, this can be more elegant solution

const obj = Object.fromEntries(sorted);
about 4 years ago · Santiago Trujillo 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!