-
哆啦的时光机
该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})
-
回首忆惘然
使用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);
-
汪汪一只猫
您可以使用该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;});
-
萧十郎
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))