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

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

未關閉
#3,765 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
HTML
星號
25.5k
分支
4k
PR 合併指標
30 天內沒有已合併 PR

描述

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;
  }, {})
}

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

開啟 javascript.info/array-methods#create-keyed-object-from-array 中的陣列方法任務,並將其中提出的分組要求與發布的 reduce 解決方案進行比較。更新文件中的解決方案,使重複的 ID 保留所有分組物件,然後驗證範例和說明仍與修正後的結果一致。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
javascript
領域
documentation
Issue 類型
缺陷
難度
2/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
描述清楚
新手友好度
50/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。