Group test results by key
beginner Array methods Challenge 4 of 18
Write groupBy(items, key). It returns an object whose keys are group names and whose values are arrays of the items in that group. You will use this kind of helper when you summarise test results by status, suite or browser.
Requirements
keyis either a property name (string) or a function that receives an item and returns its group.- Group names are converted to strings, as object keys always are.
- Items keep their original order within each group.
- An empty input returns
{}. - Do not mutate
items.
Expected output
groupBy([{ s: 'pass' }, { s: 'fail' }, { s: 'pass' }], 's') → { pass: [{ s: 'pass' }, { s: 'pass' }], fail: [{ s: 'fail' }] }
Test cases (5)
- ○groups by a property name
- ○groups by a function
- ○keeps the original order inside a group
- ○numeric keys become string keys
- ○empty input gives an empty object
solution.jsExecutes in your browser
Ctrl/⌘ + Enter to run tests · your code is saved in this browser
Results
Run the tests to see results here.