Group array by Category | The Dev Tool | Toolel.com

  const groupByCategory = products.reduce((group, product) => {

  const { category } = product; \n

  group[category] = group[category] ?? [];

  group[category].push(product);

  return group;

}, 
{});


console.log(groupByCategory);

// {
//   'fruits': [
//     { name: 'apples', category: 'fruits' }, 
//     { name: 'oranges', category: 'fruits' },
//   ],
//   'vegetables': [
//     { name: 'potatoes', category: 'vegetables' }
//   ]
// }

Created by: userf9BwjbMxsdwn8Kxu9MNW45

Comments

This page is only partially working without JavaScript. It will show content, but the tools and interactivity cannot be shown without JavaScript enabled. Please enable JavaScript for this page. About Us