SOURCE

const products = [
  { name: 'apples', category: 'fruits' },
  { name: 'oranges', category: 'fruits' },
  { name: 'potatoes', category: 'vegetables' }
];

const groupByCategory = products.reduce((group, product) => {
  const { category } = product;
  group[category] = group[category] ?? [];
  group[category].push(product);
  return group;
}, {});
console.log(groupByCategory);

// const groupByCategory = products.groupBy(product => {
//   return product.category;
// });
// console.log(groupByCategory); 
console 命令行工具 X clear

                    
>
console