How can I merge the array “product” from ingredients to the object with the same key (id) inside the object inside array “types” from the array “cookies”?
So that the array cookies contains the ingredients per type of cookie.
cookies = [
{
name: "favourites",
types: [
{ id: 1, name: "chocolate" },
{ id: 2, name: "strawberry" }
]
},
{
name: "others",
types: [
{ id: 3, name: "peanut" },
{ id: 4, name: "vanilla" }
]
}
];
ingredients = [
{
products: [
{ name: "chocolate chips", amount: "200g" },
{ name: "sugar", amount: "500g" }
],
cookieId: 1
},
{
products: [
{ name: "peanuts", amount: "350g" },
{ name: "sugar", amount: "900g" }
],
cookieId: 4
}
];
This is what it should look like:
cookies = [
{
name: "favourites",
types: [
{
id: 1,
name: "chocolate"
products: [
{ name: "chocolate chips", amount: "200g" },
{ name: "sugar", amount: "500g" }
],
},
{ id: 2, name: "strawberry" }
]
},
{
name: "others",
types: [
{
id: 3,
name: "peanut"
products: [
{ name: "peanuts", amount: "350g" },
{ name: "honey", amount: "900g" }
],
},
{ id: 4, name: "vanilla" }
]
}
];
Cheers!