Loading...
Problem Description
Write a function groupInventory(items) that takes an array of item objects. Each item has a name and a category. Your function should return a single object where the keys are the categories, and the values are arrays of item names belonging to that category.
Constraint: You must use the Array.prototype.reduce() method. Do not use a for loop or forEach.
Test Cases:
const items = [
{ name: "Apple", category: "Fruit" },
{ name: "Carrot", category: "Vegetable" },
{ name: "Banana", category: "Fruit" },
{ name: "Broccoli", category: "Vegetable" }
];
groupInventory(items);
Should return:
{
Fruit: ["Apple", "Banana"],
Vegetable: ["Carrot", "Broccoli"]
}
groupInventory([]); // Should return {}
Loading...