Initially, I have an object array
cart = [
{
"functional_id": "carton_de_10_coffrets_2_recharges_argile_offertes_coloris_rouge",
"quantity": 6
},
{
"functional_id": "identification_et_colliers_de_serrages_standard_par_50",
"quantity": 2
},
{
"functional_id": "carnet_de_conventions",
"quantity": 3
}
]
I need to compare this initial array with a nested array of objects. The nested array contains the objects from the first array and additional information needed to display the view of the app.
Here is the structure of the nested array:
market =[
{
"name": "Articles funeraires",
"functional_id": "funeral",
"generic": "incineris",
"products": [
{
"file": "data:image/;base64,",
"name": "Boîte de sympathie",
"id": 27,
"path": "",
"items": [
{
"name": "1 boîte",
"price": 0,
"functional_id": "boite_de_sympathie_1_boite"
}
]
},
...
]
Desired output:
[
{
"name": "Articles funeraires",
"products": [
"file": "data:image/;base64,",
"name": "Coffret empreinte rouge",
"path": "",
"items": [
{
"name": "Carton de 10 coffrets",
"price": 140,
"functional_id": "carton_de_10_coffrets_2_recharges_argile_offertes_coloris_rouge",
"quantity": 6
}
]
]
} ,
...
]
In summary, I need to retrieve all the product info identified by their "functional_id" while keeping the original "quantity" in the initial array of objects.
My current approach only adds properties from the "item" level to each object in the "cart" array. However, I'm struggling to construct the desired structure.
cart.forEach(cartItem => {
market.forEach(category => {
category.products.forEach(product => {
product.items.forEach(item => {
if (cartItem.functional_id === item.functional_id) {
cartItem.subtitle = item.name;
cartItem.description = item.description;
cartItem.price = item.price;
}
});
});
});
});
If you have any ideas on how to access and correct my initial approach to achieve the desired result, please let me know. Thank you in advance!