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.
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"]
total.sort((a,b) => b.score-a.score).map( x => x.name );
Array#sort, sort the array according to the scoreArray#map, return names of sorted itemsconst 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);