skip to Main Content

Here is the sample code,

let user = 
    {
        fullName: "Guest User"
        cart: 
        [
            {
                subTotal: 300,
                product: 
                [
                   {
                    productName: "apple",
                    quantity: 3
                   } 
                ]
            },
            {
                subtTotal: 1000,
                product: 
                [
                    {
                    productName: "orange",
                    quantity: 5
                    }    
                ]
            }
        ]
    }


let store =
[
    {
        name: 'apple',
        stocks: 10
    },    
    {
        name: 'orange',
        stocks: 10
    }
]

then I want to know the correct array methods to use to show the difference of store[index].stocks and user.cart[index].quantity and the result will reflect on store

I just can’t seem to know the right way to access each element in an array especially the nested ones
hope someone can help me.

Thank you in Advance! 🙂

2

Answers


  1. Check quantity for your cart items in a forEach loop and subtract it from store value. updateItemStocks is a helper function to ensure that lowest value in store’s stock will be 0.

    let user = {
      fullName: "Guest User",
      cart: [
        {
          subTotal: 300,
          product: [{ productName: "apple", quantity: 3 }]
        },
        {
          subTotal: 1000,
          product: [{ productName: "orange", quantity: 5 }]
        },
          {
          subTotal: 1000,
          product: [{ productName: "bananas", quantity:11 }]
        }
      ]
    };
    
    let store = [
      { name: 'apple', stocks: 10 },
      { name: 'orange', stocks: 10 },
      { name: 'bananas', stocks: 10 }
    ];
    
    const updateItemStocks = (stocks, qty) => stocks - qty < 0 ? 0 : stocks - qty;
    
    const updateStoreStocks = (user, store) => {
      store.forEach((item, idx) => {
        const qty = user.cart[idx]?.product?.[0]?.quantity ?? 0;
        item.stocks = updateItemStocks(item.stocks, qty);
      });
    };
    
    
    updateStoreStocks(user, store);
    console.log(store);
    Login or Signup to reply.
  2. You can first use Array#flatMap to produce an array of all the products in the user’s cart. Then, Array#reduce can be used to create an object that maps each product to its total quantity. Finally, we use Array#map to create a new array with decreased stocks based on the counts in the object from the previous step.

    let user={fullName:"Guest User",cart:[{subTotal:300,product:[{productName:"apple",quantity:3}]},{subtTotal:1e3,product:[{productName:"orange",quantity:5}]}]},store=[{name:"apple",stocks:10},{name:"orange",stocks:10}];
    let productCounts = user.cart.flatMap(x => x.product).reduce((acc, curr) => {
      acc[curr.productName] = (acc[curr.productName] || 0) + curr.quantity;
      return acc;
    }, {});
    let res = store.map(o => ({...o, stocks : o.stocks - (productCounts[o.name] || 0)}));
    console.log(res);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search