Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Group into Sections

Rendering Listsmedium

Course · Section 11: Working With Arrays · Lecture 157: The map Method

Implement `groupBySection(items, key)` returning an object that groups items by the value of `item[key]`, for rendering sectioned lists.

Sample tests

Input: groupBySection([{"cat":"a","n":1},{"cat":"b","n":2},{"cat":"a","n":3}], "cat")
Output: {"a":[{"cat":"a","n":1},{"cat":"a","n":3}],"b":[{"cat":"b","n":2}]}
Input: groupBySection([], "cat")
Output: {}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Assuming insertion order of keys matters; rely on it only when keys are strings.
  • Mutating shared group arrays from elsewhere.

Learning resources

  • React: Rendering Lists
Approach & explanation (try first)

Iterating once and bucketing each item under item[key] yields a map of arrays, ready to render as sectioned lists.

Loading...
⌘/Ctrl + Enter

Run your code to see results.