Example 1 — Official Students by Grade
Turn each score into a letter, then group the students by that letter — the sample from the Ramda docs.
const byGrade = R.groupBy((student) => {
const score = student.score;
return score < 65 ? 'F' :
score < 70 ? 'D' :
score < 80 ? 'C' :
score < 90 ? 'B' : 'A';
});
const students = [
{ name: 'Abby', score: 84 },
{ name: 'Eddy', score: 58 },
{ name: 'Dianne', score: 99 },
{ name: 'Jack', score: 69 }
];
console.log(JSON.stringify(byGrade(students)));
// { B: [Abby], F: [Eddy], A: [Dianne], D: [Jack] } How It Works
- Curry first.
R.groupBy(fn)has no list yet, so it returns the reusable helperbyGrade. - Label each student. 84 →
'B', 58 →'F', 99 →'A', 69 →'D'. - Group by label. Each student goes into the array under their letter.
- Only real keys. No one scored a C, so there is no
Ckey at all.
