Steps to get the solution: 1.Create an object named objMap and set empty data 2. Loop over array items and check objMap contains the category or not 3.If objMap doesnt contain the current category, add category name to objMap as key and assign an empty array 4.If objMap already have the category, append the category details into the array. Code :

function groupByCategory(arr) {
  const objMap={};
  arr.forEach(data => {
     if(objMap[data.category]) {
	   objMap[data.category].push(data);
	 } else {
	   objMap[data.category] = [data];
	 }
  });
  return objMap;
}

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

let result = groupByCategory(products); 
console.log('result', result); 

/* ---output ----

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