Guest User

Untitled

a guest
Dec 13th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. // 成绩等级分为A、B和C三级
  2. function getGrade(score) {
  3. return score < 60 ? 'C' :
  4. score < 80 ? 'B' : 'A';
  5. };
  6. // 学生及其成绩
  7. let students = [
  8. { name: '张三', score: 84 },
  9. { name: '李四', score: 58 },
  10. { name: '王五', score: 99 },
  11. { name: '赵六', score: 69 }
  12. ];
  13.  
  14. function groupBy(students) {
  15. return [{}].concat(students).reduce((s, c) => {
  16. const grade = getGrade(c.score);
  17. !s[grade] && (s[grade] = []);
  18. s[grade].push(c);
  19. return s;
  20. })
  21. }
  22.  
  23. // console.log(groupBy(students));
Add Comment
Please, Sign In to add comment