1 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
您不能將sort()函數(shù)用于對(duì)象。為了使您的代碼工作,您應(yīng)該轉(zhuǎn)換this.calculated為數(shù)組。
您可以使用此代碼片段將對(duì)象轉(zhuǎn)換為數(shù)組。
let calculated = {
"object1": {
"selected": false,
"value": 1
},
"object2": {
"selected": false,
"value": 20
},
"object3": {
"selected": false,
"value": 4
},
"object4": {
"selected": false,
"value": 24
},
"object5": {
"selected": false,
"value": 6
},
"object6": {
"selected": false,
"value": 0.26
},
"object7": {
"selected": true,
"value": 1.52
},
"object8": {
"selected": false,
"value": 0.54
},
"object9": {
"selected": false,
"value": 4.27
}
}
let calculatedArray = []
for (const [key, value] of Object.entries(calculated)) {
calculatedArray.push({
id: key,
...value
})
}
sortedData = function() {
if(calculatedArray) {
return calculatedArray.sort(function(a, b) {
return b.selected - a.selected
})
} else {
return []
}
}
console.log(sortedData())
console.log(calculatedArray)
添加回答
舉報(bào)