我有一个如下所示的对象数组:
[{
"id": 1,
"Size": 90,
"Maturity": 24,
},
{
"id": 2,
"Size": 85,
"Maturity": 22,
},
{
"id": 3,
"Size": 80,
"Maturity": 20,
}]
我需要基于不同的属性值排序(例如:成熟度)对此数组进行操作,并添加具有升序/等级的列顺序。 例如:
[{
"id": 1,
"Size": 90,
"Maturity": 22,
"Order": 2
},
{
"id": 2,
"Size": 85,
"Maturity": 25,
"Order": 3
},
{
"id": 3,
"Size": 80,
"Maturity": 20,
"Order": 1
}]
答案 0 :(得分:1)
.overlay-startpage {
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
z-index: 0;
-ms-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}
答案 1 :(得分:0)
使用sort
对数组进行排序,然后针对每个对象使用forEach
对其进行排序的索引添加prop:
var inp = [{
id: 1,
Size: 90,
Maturity: 24,
},
{
id: 2,
Size: 85,
Maturity: 22,
},
{
id: 3,
Size: 80,
Maturity: 20,
}]
// Sort
inp.sort(function(a, b){
return a.Maturity == b.Maturity ? 0 : +(a.Maturity > b.Maturity) || -1;
});
// add prop
inp.forEach(function(row, index) {
row.index = index + 1;
});
console.log(inp)
答案 2 :(得分:0)
var objs = [
{
"id": 1,
"Size": 90,
"Maturity": 24,
},
{
"id": 2,
"Size": 85,
"Maturity": 22,
},
{
"id": 3,
"Size": 80,
"Maturity": 20,
}];
function compare(a,b) {
if (a.Size < b.Size)
return -1;
if (a.Size > b.Size)
return 1;
return 0;
}
objs.sort(compare);
for (var i = 0; i < objs.length; i++) {
objs[i].Order = i+1;
}
console.log(objs);