我有一些資料需要使用每個物件的第一列進行排序。
該專案使用 Angular / Typescript,但它仍然是 JS。
以下是資料的外觀:
[
{
time: 1000.189,
other: 100
},
{
time: 1023.189 ,
other: 105
},
{
time: 999.189,
other: 100
}
]
所以上面應該是這樣的:
[
{
time: 999.189,
other: 100
},
{
time: 1000.189,
other: 100
},
{
time: 1023.189,
other: "105
}
]
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
function sortByColumn(colname){
return array.sort((a,b)=>a[colname]-b[colname])
}
const array = [
{
time: 1000.189,
other: 100
},
{
time: 1023.189 ,
other: 105
},
{
time: 999.189,
other: 100
}
]
const orderSorted = sortByColumn('order')
const timeSorted = sortByColumn('time')
console.log(orderSorted)
console.log(timeSorted)
uj5u.com熱心網友回復:
this.array.sort((a, b) => a['time'] - b['time']);
uj5u.com熱心網友回復:
const array = [
{
time: 1000.189,
other: 100
},
{
time: 1023.189 ,
other: 105
},
{
time: 999.189,
other: 100
}
]
const result = array.sort((a, b) => a.time - b.time);
console.log(result);
結果= [{時間:999.189,其他:100},{時間:1000.189,其他:100},{時間:1023.189,其他:105},]
這適用于 Angular/Javascript
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/527569.html
