この解答例はChatGPTによって生成されています,正しいかは自己判断で。 皆の投稿からも解答例が見つかるかもしれませんよ。
BFE.devでのAI解答例 - JavaScript コーディング問題
177. Implement Object.groupBy()
Object.groupBy = function(items, keyFunc) { return items.reduce((acc, item) => { const key = keyFunc(item); if (!acc[key]) { acc[key] = []; } acc[key].push(item); return acc; }, {});};const items = [ { id: 1, kind: 'a', }, { id: 2, kind: 'b', }, { id: 3, kind: 'a', }]const groups = Object.groupBy(items, ({kind}) => kind);console.log(groups);
// Output// {// a: [// { id: 1, kind: 'a' },// { id: 3, kind: 'a' }// ],// b: [// { id: 2, kind: 'b' }// ]// }