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

181
Views
JavaScript: sort and map array of objects
const obj = {
  "total": [{
      "name": "asdf",
      "score": 8
    },
    {
      "name": "zxcv",
      "score": 4
    },
    {
      "name": "qwer",
      "score": 17
    },
    {
      "name": "poiu",
      "score": 8
    },
    {
      "name": "lkjh",
      "score": 6
    }
  ]
}
// expected
// ["qwer", "asdf", "poiu", "lkjh", "zxcv"]

Do you know how to arrange it like this?

The sorting criteria are in the descending order of the "score" value.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const obj = {
  "total": [{
      "name": "asdf",
      "score": 8
    },
    {
      "name": "zxcv",
      "score": 4
    },
    {
      "name": "qwer",
      "score": 17
    },
    {
      "name": "poiu",
      "score": 8
    },
    {
      "name": "lkjh",
      "score": 6
    }
  ]
}

const res = obj["total"].sort((i, j) => j.score - i.score).map((i)=>i.name);
console.log(res);
// expected
// ["qwer", "asdf", "poiu", "lkjh", "zxcv"]

about 4 years ago · Juan Pablo Isaza Report

0

total.sort((a,b) => b.score-a.score).map( x => x.name );

about 4 years ago · Juan Pablo Isaza Report

0

  • Using Array#sort, sort the array according to the score
  • Using Array#map, return names of sorted items

const total =  [ { "name": "asdf", "score": 8 }, { "name": "zxcv", "score": 4 }, { "name": "qwer", "score": 17 }, { "name": "poiu", "score": 8 }, { "name": "lkjh", "score": 6 } ];

const sortedNames = total
  .sort(({ score: a }, { score: b }) => b - a)
  .map(({ name }) => name);

console.log(sortedNames);

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!