2

I have only been programming for a short time and I am faced with a data structure that is difficult to understand.I have a function where I get an array of objects, representing products added to a cart.

  getAllAddedItems(addedItems) {

    this.fullInfoAddedItems.push(addedItems);
    const filtered = this.fullInfoAddedItems.reduce((filtered, item) => {
      if ( !filtered.some(filteredItem => JSON.stringify(filteredItem) === JSON.stringify(item)) ) {
        filtered.push(item);
      }
      return filtered;
    }, []);
    this.fullInfoAddedItems = filtered;
}


As a result I get the following array of objects

[
  {
    "price": 0,
    "functional_id": "carnet_de_conventions",
    "quantity": 3
  },
  {
    "price": 0,
    "functional_id": "depliants_services_incinération",
    "quantity": 4
  }
]

The problem is that I need a data "name" that is in an array of nested objects to complete the view information.

This would be the structure of the array

[

{
  "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"
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Coffret empreinte rouge",
      "id": 8,
      "path": "",
      "items": [
        {
          "name": "Carton de 10 coffrets",
          "price": 140,
          "functional_id": "carton_de_10_coffrets_2_recharges_argile_offertes_coloris_rouge"
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "AfuBOX",
      "id": 10,
      "path": "",
      "items": [
        {
          "name" : "PACK N°1 comprenant :",
          "price": 30,
          "functional_id": "afubox_6_petits_modeles_4_moyens_modeles",
          "quantity": 4
        },
        {
          "name" : "PACK N°2 comprenant :",
          "price": 70,
          "functional_id": "afubox_6_petits_modeles_4_moyens_modeles",
          "quantity": 6
        }

      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Catalogue d'urnes décoratives",
      "id": 20,
      "path": "",
      "items": [
        {
          "price": 0,
          "functional_id": "catalogue_urnes_decoratives"
        }
      ]
    }
  ],
  "sorting": 2200
},
  {
  "name": "Documents",
  "functional_id": "incineris_doc",
  "generic": "incineris",
  "products": [
    {
      "file": "data:image/;base64,",
      "name": "Carnet de conventions",
      "id": 17,
      "path": "",
      "items": [
        {
          "price": 0,
          "functional_id": "carnet_de_conventions",
          "quantity": 3
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Affiches procédure",
      "description": "De prise en charge et de crémation des animaux",
      "id": 18,
      "path": "",
      "items": [
        {
          "price": 0,
          "functional_id": "affiches_procedure_de_prise_en_charge_et_de_cremation_des_animaux"
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Dépliants services de crémation",
      "description": "Pour animaux de compagnie",
      "id": 19,
      "path": "",
      "items": [
        {
          "price": 0,
          "functional_id": "depliants_services_incinération",
          "quantity": 4
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Catalogue d'urnes décoratives",
      "id": 20,
      "path": "",
      "items": [
        {
          "price": 0,
          "functional_id": "catalogue_urnes_decoratives"
        }
      ]
    }
  ],
  "sorting": 2400
},
{
  "name": "Matériel crémation",
  "functional_id": "furniture",
  "generic": "incineris",
  "products": [
    {
      "file": "data:image/;base64,",
      "name": "Sacs blancs",
      "description": "Pour les crémations Plurielles",
      "id": 11,
      "path": "",
      "items": [
        {
          "name": "Petit modèle",
          "description": "Par 25",
          "price": 0,
          "functional_id": "sacs_blancs_pour_les_cremations_plurielles_petit_modele_par_25"
        },
        {
          "name": "Moyen modèle",
          "description": "Par 20",
          "price": 0,
          "functional_id": "sacs_blancs_pour_les_cremations_plurielles_moyen_modele_par_20"
        },
        {
          "name": "Grand modèle",
          "description": "Par 10",
          "price": 0,
          "functional_id": "sacs_blancs_pour_les_cremations_plurielles_grand_modele_par_10"
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Sacs bordeaux",
      "description": "Pour les crémations Référence",
      "id": 12,
      "path": "",
      "items": [
        {
          "name": "Petit modèle",
          "description": "Par 25",
          "price": 0,
          "functional_id": "sacs_bordeaux_pour_les_cremations_reference_petit_modele_par_10"
        },
        {
          "name": "Grand modèle",
          "description": "Par 10",
          "price": 0,
          "functional_id": "sacs_bordeaux_pour_les_cremations_reference_grand_modele_par_10"
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Sacs verts",
      "description": "Pour les crémations Privées",
      "id": 13,
      "path": "",
      "items": [
        {
          "name": "Petit modèle",
          "description": "A l'unité",
          "price": 0,
          "functional_id": "sacs_verts_pour_les_cremations_privees_petit_modele_unite"
        },
        {
          "name": "Moyen modèle",
          "description": "A l'unité",
          "price": 0,
          "functional_id": "sacs_verts_pour_les_cremations_privees_moyen_modele_unite"
        },
        {
          "name": "Grand modèle",
          "description": "A l'unité",
          "price": 0,
          "functional_id": "sacs_verts_pour_les_cremations_privees_grand_modele_unite"
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Sacs bleus",
      "description": "Pour pièces anatomiques",
      "id": 14,
      "path": "",
      "items": [
        {
          "name": "Standard",
          "description": "Par 25",
          "price": 0,
          "functional_id": "sacs_bleus_pour_pieces_anatomiques_standard_par_25"
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Etiquettes d'identification",
      "id": 15,
      "path": "",
      "items": [
        {
          "name": "Standard",
          "description": "Par 50",
          "price": 0,
          "functional_id": "etiquettes_identification_et_colliers_de_serrages_standard_par_50"
        }
      ]
    },
    {
      "file": "data:image/;base64,",
      "name": "Colliers de serrages",
      "id": 16,
      "path": "",
      "items": [
        {
          "name": "Standard",
          "description": "Par 50",
          "price": 0,
          "functional_id": "distributeurs_pour_sacs_housse_par_5"
        }
      ]
    }
  ],
  "sorting": 2300
}
]
[
  {
    "price": 0,
    "functional_id": "carnet_de_conventions",
    "quantity": 3,
    "name": "Carnet de conventions"
  },
  {
    "price": 0,
    "functional_id": "depliants_services_incinération",
    "quantity": 4,
    "name": "Dépliants services de crémation"
  }
]

I attach a stacblitz link simulating what I'm trying to do to compare by "functional_id" to get "name" from the array of nested objects and push to the first array

https://stackblitz.com/edit/angular-ivy-3wfgme

Someone to give me an effective option to get it. Thank you in advance.

what I try to do is go through the second array to compare the "functional_id" of the original product with the "functional_id" of the product added to the first array, so I can extract the "name" in the first array and push it into the first array

this.market.forEach(function(item: any, i) {
      item.products.forEach(function(products: any, i) {
      products.items.forEach(function(sale: any, i) {
        let saleId = sale.functional_id;
        console.log(sale);
        console.log(saleId);

      });

      });
    });

This is what I'm trying to do, but I don't see how I can compare the two arrays

homerThinking
  • 785
  • 3
  • 11
  • 28

2 Answers2

2
for (var j = 0; j <= nestedArray.length - 1; j++) {
  for (var k = 0; k <= nestedArray[j]['products'].length - 1; k++) {
    for (var i = 0; i <= inputArray.length - 1; i++) {
      let pos = nestedArray[j]['products'][k]['items'].map(ele => ele.functional_id).indexOf(inputArray[i]['functional_id']);
      if (pos != -1) {
        inputArray[i]['name'] = nestedArray[j]['products'][k]['name'];
      }
    }
  }
}
Ajai
  • 351
  • 2
  • 10
1

Iterate over the addedItems array and for each item in this array, iterate over the products array in each object inside product array and check if any of the item inside current items array has a functional_id equal to the functional_id of the current item from the addedItems array

getAllAddedItems(addedItems) {

  addedItems.forEach(item => {
     this.product.forEach(({products}) => {
        products.forEach(({name, items}) => {
           const index = items.findIndex(i => i.functional_id === item.functional_id);
           if (index > -1) item.name = name;
        })
     })
  })

  console.log(addedItems);
}

Here's a stackblitz demo. Open the console to see the output of the function

Yousaf
  • 27,861
  • 6
  • 44
  • 69
  • Works perfect! I see I was doing it the other way around and in a much more complicated way... thank you very much for your time and your help – homerThinking May 29 '20 at 13:34