I am trying to construct an array based on two separate arrays such as the following example:
const breakfast = [
{ dishId: 23, name: 'Pasta'}
]
const ingredients = [
// ...
{ ingrId: 13, name: 'Tomato' },
{ ingrId: 29, name: 'Beef' }
]
// connecting table
const breakfastDishIngredients = [
{ id: 1, dishId: 23, ingrId: 13 },
{ id: 1, dishId: 23, ingrId: 29 }
]
The new array should be the breakfast array with an additional ingredients key for every element. Like this:
const newBreakfast = [
{ dishId: 23, name: 'Pasta', ingredients: [
{ ingrId: 13, name: 'Tomato' },
{ ingrId: 29, name: 'Beef' }
]}
]
I am trying the following but its not working:
let newBreakfast = []
for(let i in breakfastDishIngredients) {
_breakfast = breakfast.map(item => {
return { ...item, ingredients: ingredients.filter(el => item.id === breakfastDishIngredients[i][0].dish_id && el.id === breakfastDishIngredients[i][0].ingredient_id) }
})
}
I appreciate any help.