4 回答
TA贡献1779条经验 获得超6个赞
该sort函数应该执行以下操作:
var raw_scores = [
{
"score": 51,
"name": "toto"
},
{
"score": 94,
"name": "tata"
},
{
"score": 27,
"name": "titi"
},
{
"score": 100,
"name": "tutu"
}
]
var sorted_scores = raw_scores.sort(function(a,b){return b.score - a.score})
TA贡献1847条经验 获得超11个赞
使用for循环
var index = 0;
var max = 0;
for (var i = 0; i < scores.length; i++) {
if (s[i].score > max) {
max = s[i].score;
index = i;
}
}
console.log(index);
TA贡献1898条经验 获得超8个赞
您可以使用该reduce功能
const array = [
{
"score": 51,
"name": "toto"
},
{
"score": 94,
"name": "tata"
},
{
"score": 27,
"name": "titi"
},
{
"score": 100,
"name": "tutu"
}
];
const highestScore = array.reduce((last, item) => {
// return the item if its score is greater than the highest score found.
if(!last || last.score < item.score) {
return item;
}
return last;
});
TA贡献1815条经验 获得超13个赞
var data = [{
"score": 51,
"name": "toto"
},
{
"score": 94,
"name": "tata"
},
{
"score": 27,
"name": "titi"
},
{
"score": 100,
"name": "tutu"x
}
];
var max_score = Math.max.apply(Math, data.map(function(o) {
return o.score;
}))
console.log(data.filter(i => i.score === max_score))
添加回答
举报