I cannot seem to figure out why I am getting an error when trying to do some object transformation.
What I am trying to do is take an array of items and using the Array.Reduce method create a new object based off a field within the item. After that I am using the Object.entries.map method to create a new array so I can later iterate over the objects. Code is below.
export interface IMenuItem {
id: string;
name: string;
description: string;
category: string;
imageUrl: string;
quantity: number;
}
function toItemGroups(items: IMenuItem[]): [{ items: {}; label: string }] {
const groups: {} = items.reduce((result, item) => {
const group: [{}] = result[item.category] || [];
result[item.category] = group.concat(item);
return result;
}, {});
return Object.entries(groups).map(([label, items]) => ({ items, label }));
}
The error that I am getting is:
type { items: IMenuItem[]; label: string }[] is not assignable to type [{ items: {}; label: string }].
This is pretty self explanatory but I cannot seem to figure out what types I need to get this to map correctly.
Any suggestions would be greatly appreciated.
IMenuItemwhilst on the other its a simple object?[{ items: {}; label: string }]instead of{ items: {}; label: string }[]?