var data = [
{ StudentNumber: 2022003, Chinese: 86, Math: 64, English: 80, TotalScore: "", Comment: "" },
{ StudentNumber: 2022004, Chinese: 78, Math: 99, English: 91, TotalScore: "", Comment: "" },
{ StudentNumber: 2022005, Chinese: 107.5, Math: 97, English: 70, TotalScore: "", Comment: "" },
{ StudentNumber: 2022006, Chinese: 112, Math: 61, English: 92, TotalScore: "", Comment: "" },
{ StudentNumber: 2022007, Chinese: 101, Math: 79, English: 104, TotalScore: "", Comment: "" },
{ StudentNumber: 2022008, Chinese: 71, Math: 72, English: 105, TotalScore: "", Comment: "" },
{ StudentNumber: 2022009, Chinese: 56, Math: 68, English: 61, TotalScore: "", Comment: "" },
{ StudentNumber: 2022010, Chinese: 98, Math: 83, English: 77, TotalScore: "", Comment: "" }
];
并根据总成绩计算每个学生成绩备注保存在 Comment 中,110分以上为A,100分以上为B,90分以上为C,72分以上为D,72分以下为E
var data = [ { StudentNumber: 2022003, Chinese: 86, Math: 64, English: 80, TotalScore: "", Comment: "" }, { StudentNumber: 2022004, Chinese: 78, Math: 99, English: 91, TotalScore: "", Comment: "" }, { StudentNumber: 2022005, Chinese: 107.5, Math: 97, English: 70, TotalScore: "", Comment: "" }, { StudentNumber: 2022006, Chinese: 112, Math: 61, English: 92, TotalScore: "", Comment: "" }, { StudentNumber: 2022007, Chinese: 101, Math: 79, English: 104, TotalScore: "", Comment: "" }, { StudentNumber: 2022008, Chinese: 71, Math: 72, English: 105, TotalScore: "", Comment: "" }, { StudentNumber: 2022009, Chinese: 56, Math: 68, English: 61, TotalScore: "", Comment: "" }, { StudentNumber: 2022010, Chinese: 98, Math: 83, English: 77, TotalScore: "", Comment: "" } ]; function ScoreHandling(data){ this.data=data; } ScoreHandling.prototype.scoreSort=function(data,subject,subjectText){ data.sort(function(a,b){ return b[subject]-a[subject]; }) document.write(subjectText + '第一名是:' + data[0].StudentNumber +'   '+ '成绩为:' + data[0][subject] + '<br>'); } ScoreHandling.prototype.outputScore=function(){ let _data=this.data; for(let stu of _data){ stu.TotalScore=stu.Chinese+stu.Math+stu.English; var stuscore=(stu.TotalScore/3).toFixed(1); if(stuscore>=110){ stu.Comment='A'; } else if(stuscore>=100){ stu.Comment='B'; } else if(stuscore>=90){ stu.Comment='C'; }else if(stuscore>=72){ stu.Comment='D'; }else { stu.Comment='E'; } } console.log(_data); this.scoreSort(_data,'TotalScore','总成绩'); this.scoreSort(_data,'Chinese','语文'); this.scoreSort(_data,'Math','数学'); this.scoreSort(_data,'English','英语'); } var score=new ScoreHandling(data); score.outputScore();
运行截图如下: