score: [{ 'name': 'Zhexue', 'score': 62 },
{ 'name': 'Jingjixue', 'score': 33 },
{ 'name': 'Faxue', 'score': 52 },
{ 'name': 'Jiaoyuxue', 'score': 44 },
{ 'name': 'Shuxue', 'score': 47 },
{ 'name': 'Wenxue', 'score': 87 },
{ 'name': 'Xinlixue', 'score': 57 },
{ 'name': 'Yixue', 'score': 43 },
{ 'name': 'Guanlixue', 'score': 47 },
{ 'name': 'Yishuxue', 'score': 52 },
{ 'name': 'Lixue', 'score': 77 },
{ 'name': 'Gongxue', 'score': 79 },
{ 'name': 'Lishixue', 'score': 88 }
]
请问如何定义函数取到score.score中的最大值,并取出name
function getmaxscore(arrayobj)
var maxi=0;
for (var i=0;i<arrayobj.length;i++)
{
if (arrayobj[maxi].score<=arrayobj[i].score)
{
maxi=i;
}
}
return maxi;
}
代码如上所示,希望帮到你
下载underscore.js (https://cdn.bootcss.com/underscore.js/1.8.3/underscore.js)
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.max(stooges, function(stooge){ return stooge.age; });
=> {name: 'curly', age: 60};
var max = {
name: '',
score: 0
};
for (var i = 0;i < score.length; i++) {
var item = score[i];
if (item.score > max.score) {
max.score = item.score;
max.name = item.name
}
}
max => { 'name': 'Lishixue', 'score': 88 }
写一个对象比较函数如下所示,然后获得最大或者最小即可
//比较器
function compare(propertyName) {
return function (object1, object2) {
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value1 < value2) {
return 1;
}else if (value1 > value2) {
return -1;
}else {
return 0;
}
};
}
//使用方法
data.sort(compare("name"));
var scoremax = score[0].score;
var namemax = score[0].name;
for(var i=1;i<score.length;i++){
if(score[i].score>scoremax){
scoremax = score[i].score;
namemax = score[i].name;
}
}
console.info("name:"+namemax+",score:"+scoremax);
办法可能笨一点,先得出最大分数
var maxscore=maxscore=data.score[0].score;
for( var i =0;i++;i if(data.score[i+1].score>maxscore){
maxscore=data.score[i+1].score;
}
}
然后根据分数得出name
for( var i =0;i++;i<data.score.length–1){
if(data.score[i].score=maxscore){
alert(data.score[i].name);
}
}
function getmaxscore(arrayobj)
var maxi=0;
for (var i=0;i<arrayobj.length;i++)
{
if (arrayobj[maxi].score<=arrayobj[i].score)
{
maxi=i;
}
}
return maxi;
}
代码如上所示,希望帮到你
这个没什么错
function getMaxScore(obj)
{
}
Math.max.app(Maht,score.map(input=>{return input.score}))