我有如下的对象数组:
[{
"id": 1,
"Size": 90,
"Maturity": 24,
},
{
"id": 2,
"Size": 85,
"Maturity": 22,
},
{
"id": 3,
"Size": 80,
"Maturity": 20,
}]
我需要基于不同属性值排序(例如:成熟度)的此Array,还需要添加具有升序/等级的列顺序。
例如:
[{
"id": 1,
"Size": 90,
"Maturity": 22,
"Order": 2
},
{
"id": 2,
"Size": 85,
"Maturity": 25,
"Order": 3
},
{
"id": 3,
"Size": 80,
"Maturity": 20,
"Order": 1
}]
最佳答案
const arr = [{
"id": 1,
"Size": 90,
"Maturity": 24,
},
{
"id": 2,
"Size": 85,
"Maturity": 22,
},
{
"id": 3,
"Size": 80,
"Maturity": 20,
}];
arr
.map((item,index) => ({ ...item, Order: index + 1 }))
.sort((a, b) => b.Maturity - a.Maturity)