Alexy Riche
Alexy Riche

Reputation: 27

How to get an object with its ID?

I'm creating a food app which we can customize food like subway. I save items (like sandwich or Salami) and their ingredients (like mustard or bbq) in the shopping cart thank's to their IDs.

The data in my shopping cart are saved like that :

menuSelection: MenuSelection = {
  id: 0,
  supp: [1123],
};

The menu looks like this :

  products: Menu[] = [
    {
        id: 0, name: 'Salami', price: 8, hour: null,
        supp:[
          { id: 1123, name: 'fromage', price: 5, hour: null },
          { id: 11234, name: 'bbq', price: 10, hour: null },
        ],
    },
  ];

So in my shopping cart I just have the ids and I want to get the object of the food (item and ingredients) how I can do that please?

Upvotes: 0

Views: 353

Answers (3)

Raviteja V
Raviteja V

Reputation: 344

In this to get the product from the menu you can use

findProductById = (id: number) => this.products.filter(product => product.id === id);

if you want look into ingredients of that also then

findProductById = (id: number) => {
   this.products.filter(
      product => 
      product.id === id || 
      product.supp.filter(ingredients => ingredients.id === id).length
   );
}

product.supp.filter(ingredients => ingredients.id === id).length this will return length, if length is 0 not match found so false or else length is greater than 0 in case of match found.

If this condition (product.id === id) is true then product is found at the top level.

Upvotes: 1

Nikhil
Nikhil

Reputation: 6643

You have an array of MenuSelection objects in your shopping cart.

To get the product and ingredients from each MenuSelection, you can try the following.

You didn't mention how you want to store the result, so I'm just displaying them on the screen. You can insert them into an array or an object according to your needs.

var products = [
    {
        id: 0, name: 'Salami', price: 8, hour: null,
        supp:[
          { id: 1123, name: 'fromage', price: 5, hour: null },
          { id: 11234, name: 'bbq', price: 10, hour: null },
        ]
    },
    {
        id: 1, name: 'Fish', price: 10, hour: null,
        supp:[
          { id: 1123, name: 'fromage', price: 5, hour: null },
          { id: 11234, name: 'bbq', price: 10, hour: null },
        ]
    }
  ];

var shoppingCart = [{
  id: 0,
  supp: [1123],
},
{
  id: 1,
  supp: [1123, 11234],
}];

shoppingCart.forEach(menuSelection => {
  let product = products.find(item => item.id === menuSelection.id);
  let foodItem = product.name;
  let ingredients = [];
  
  menuSelection.supp.forEach(suppId => {
    ingredients.push(product.supp.find(item => item.id === suppId));
  });
   
  console.log("Food Item: " + foodItem);
   
  console.log("Ingredients: ");
  ingredients.forEach(item => {
    console.log(item);
  });
});

Upvotes: 1

mbojko
mbojko

Reputation: 14679

So, you have an id and looking for the associated product in the products array? It should be something along the lines

findProductById = (id: number) => this.products.find(product => product.id === id);

Upvotes: 1

Related Questions