我的数据对象结构如下
var maxObject = _.max(data,function(obj){
return parseInt(obj.Points) && parseFloat(obj.Rating)
});
console.log(maxObject.Person);
我使用下面的underscore.js获得结果
<span class="arc-gauge-title">{{title | translate}}</span>
<app-change-pages-dropdown [pageTitle]="pageTitle | translate"></app-change-pages-dropdown>
以上代码结果为“Kay”,但我希望得到“Micheal”
的结果答案 0 :(得分:0)
我找到了答案。工作代码如下;
data = [
{
Points: "13",
Rating: "-2",
Person: "Joe"
},
{
Points: "13",
Rating: "200",
Person: "Kay"
},
{
Points: "17",
Rating: "32.8",
Person: "Micheal"
}];
// sort version
var maxObject = _.first(data.sort(function (a, b) {
return parseFloat(a.Points) - parseFloat(b.Points) || parseFloat(a.Rating) - parseFloat(b.Rating);
}).reverse());
// long version
data.sort(function (a, b) {
var aSize = parseFloat(a.Points);
var bSize = parseFloat(b.Points);
var aLow = parseFloat(a.Rating);
var bLow = parseFloat(b.Rating);
console.log(aLow + " | " + bLow);
if(aSize == bSize)
{
return (aLow < bLow) ? -1 : (aLow > bLow) ? 1 : 0;
}
else
{
return (aSize < bSize) ? -1 : 1;
}
}).reverse();
alert(JSON.stringify(maxObject));