代码:
function orderStudentGrade(students) {
// TODO: 在这里写入具体的实现逻辑
// 将学生的成绩按班级分组,同一班级中按照总分从高到底排序
if(students.length == 0) {
return {}
}
// 排序函数,按照总分降序
const compare = (a, b) => {
const scoreA = a.math + a.language + a.english + a.physics + a.chemistry
const scoreB = b.math + b.language + b.english + b.physics + b.chemistry
return scoreB - scoreA
}
let result = {}
let arr1 = students.filter(item => item.class == 1).sort(compare) //留下班级为1的数组
let arr2 = students.filter(item => item.class == 2).sort(compare)
let arr3 = students.filter(item => item.class == 3).sort(compare)
result = { 1:arr1, 2:arr2, 3:arr3}
return result
}
module.exports = orderStudentGrade; // 检测需要,请勿删除
考点:
1.数组排序:
arr.sort(compareFn)
基础升序函数
(a,b) => return a-b
按数组内对象内某个属性排序
function compare(a, b) {
return (a.math + a.english) - (b.math + b.english)
}
2.过滤器:
常用于筛选数组
let arr = arr.filter(function(currentValue, index, arr) {
return //满足条件直接return,用新数组接收
})