Here is a list of objects that I have:
enum MealType {
Breakfast,
Lunch,
Dinner
}
interface FoodItem {
name: string,
type: MealType[],
}
const foodItems: FoodItem[] = [
{
name: 'Pizza',
type: [MealType.Lunch, MealType.Dinner]
},
{
name: 'Salad',
type: [MealType.Lunch]
},
{
name: 'Oatmeal',
type: [MealType.Breakfast]
}
];
I would like to group my foodItems
by type
, so the desired output is as follows:
{
'Breakfast': [
{
name: 'Oatmeal',
type: ['Breakfast']
}
],
'Lunch': [
{
name: 'Pizza',
type: ['Lunch', 'Dinner']
},
{
name: 'Salad',
type: ['Lunch']
}
],
'Dinner': [
{
name: 'Pizza',
type: ['Lunch', 'Dinner']
}
]
};