javascript-tutorial / javascript-tutorial/en.javascript.info

Bug in solution of this task: https://javascript.info/array-methods#create-keyed-object-from-array

Open
#3,765 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
25.5k
Forks
4k
PR merge metrics
No merged PRs in 30d

Description

There's a bug in the solution posted in this task.
The task requires us to group objects by id and put the grouped objects in an array keyed by id.
The solution posted is:

function groupById(array) {
  return array.reduce((obj, value) => {
    obj[value.id] = value;
    return obj;
  }, {})
}

However, this does not work when there are items in array with same id.
To accomplish the task correctly, the solution should be:

function groupById(array) {
  return array.reduce((obj, value) => {
    obj[value.id] == undefined ? obj[value.id] = [value] : obj[value.id].push(value);
    return obj;
  }, {})
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the array-methods task at javascript.info/array-methods#create-keyed-object-from-array and compare the stated grouping requirement with the posted reduce solution. Update the documented solution so repeated IDs retain all grouped objects, then verify the example and explanation still match the corrected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
documentation
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.