以下为ChatGPT自动生成的解答,正确与否请自行判断。 也许你也可以从大家的发帖中找到想要的答案!

BFE.devAI解答 - JavaScript 代码题
177. Implement Object.groupBy()

Here is an implementation of Object.groupBy():

Object.groupBy = function(array, keyFunc) {  return array.reduce((result, item) => {    const key = keyFunc(item);    if (!result[key]) {      result[key] = [];    }    result[key].push(item);    return result;  }, {});}const items = [  {    id: 1,    kind: 'a',  },  {    id: 2,    kind: 'b',  },  {    id: 3,    kind: 'a',  }]const groups = Object.groupBy(items, ({kind}) => kind)console.log(groups);

This implementation uses the reduce method to iterate through the array of items and group them by the result of the keyFunc function. The keyFunc function is passed as a callback to specify the key for grouping. The result is an object where the keys represent the grouping criteria and the values are arrays of items belonging to each group.