const items = [
{ id: 0, category: undefined },
{ id: 1, category: 'food' },
{ id: 2, category: 'drink' },
{ id: 3, category: 'food' },
{ id: 4, category: '' },
{ id: 5, category: null },
];
 
function groupByCategory(groups) {
const result = {};
 
for (let i = 0; i < groups.length; i++) {
const category = items[i].category;
if (!category) continue;
 
if (!result[groups[i].category]) {
result[groups[i].category] = [];
}
 
result[groups[i].category].push(groups[i]);
}
 
return result;
}
 
console.log(groupByCategory(items));
 
Code will run automatically after changes.
Code runs in an isolated environment.