Home > database >  Javascript Array value not updaing
Javascript Array value not updaing

Time:01-14

I think I'm having trouble understanding the fundamentals of updating an array in Javascript. I have a cope snippet that is trying to do something to the effect of what I'll paste below.

I'll update the value of something in request.quantities, then the next reference to it takes the original value instead of the updated one. Can someone please explain to me where my disconnect is?

For example, the expected output for 'order' from this function should be:

[
{name: 'apple', batch: '12345', quantity: 5},
{name: 'orange', batch: '23456', quantity: 10},
{name: 'banana', batch: '34567', quantity: 3},
{name: 'banana', batch: '99999', quantity: 12},
]

But it winds up being:

[
{name: 'apple', batch: '12345', quantity: 5},
{name: 'orange', batch: '23456', quantity: 10},
{name: 'banana', batch: '34567', quantity: 3},
{name: 'banana', batch: '99999', quantity: 15},
]
        let request = {
            products:['apple', 'orange', 'banana'], 
            quantities:[5, 10, 15]
        }

        let stock = [
            {name: 'apple', batch: '12345', quantity: 50},
            {name: 'orange', batch: '23456', quantity: 1000},
            {name: 'banana', batch: '34567', quantity: 3},
            {name: 'banana', batch: '99999', quantity: 500},
        ]

        let order = []

        for (let i = 0; i < request.products.length; i  ) {
            for (let j = 0; j < stock.length; j  ) {
                if (request.products[i] == stock[j].name) {
                    if (stock[j].quantity - request.quantities[i] > 0) {
                        order.push({name: request.products[i], batch: stock[j].batch, quantity: request.quantities[i]})
                        stock[j].quantity = stock[j].quantity - request.quantities[i]
                        request.quantities[i] = 0
                    } else {
                        order.push({name: request.products[i], batch: stock[j].batch, quantity: stock[j].quantity})
                        stock[j].quantity = 0
                        request.quantities[i] = request.quantities[i] - stock[j].quantity
                    }
                }
            }
        }

CodePudding user response:

There's just a small logic error here. You need to set the request quanities in your else statement before you set the stock for that item quantity to 0.

const request = {
  products: ['apple', 'orange', 'banana'],
  quantities: [5, 10, 15],
};

const stock = [
  { name: 'apple', batch: '12345', quantity: 50 },
  { name: 'orange', batch: '23456', quantity: 1000 },
  { name: 'banana', batch: '34567', quantity: 3 },
  { name: 'banana', batch: '99999', quantity: 500 },
];

const order = [];

for (let i = 0; i < request.products.length; i  ) {
  for (let j = 0; j < stock.length; j  ) {
    if (request.products[i] == stock[j].name) {
      if (stock[j].quantity - request.quantities[i] > 0) {
        order.push({
          name: request.products[i],
          batch: stock[j].batch,
          quantity: request.quantities[i],
        });
        stock[j].quantity = stock[j].quantity - request.quantities[i];
        request.quantities[i] = 0;
      } else {
        order.push({
          name: request.products[i],
          batch: stock[j].batch,
          quantity: stock[j].quantity,
        });
        /* Set request quantity first */
        request.quantities[i] = request.quantities[i] - stock[j].quantity;
        stock[j].quantity = 0;
      }
    }
  }
}

console.log(order)

  • Related