Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 成绩等级分为A、B和C三级
- function getGrade(score) {
- return score < 60 ? 'C' :
- score < 80 ? 'B' : 'A';
- };
- // 学生及其成绩
- let students = [
- { name: '张三', score: 84 },
- { name: '李四', score: 58 },
- { name: '王五', score: 99 },
- { name: '赵六', score: 69 }
- ];
- function groupBy(students) {
- return [{}].concat(students).reduce((s, c) => {
- const grade = getGrade(c.score);
- !s[grade] && (s[grade] = []);
- s[grade].push(c);
- return s;
- })
- }
- // console.log(groupBy(students));
Add Comment
Please, Sign In to add comment