-1

How can I merge my array of objects with same value? I have got orders array, which may have the same product. If so, I want to merge them and add the quantity.

var orders = [
      {
        product: "chair",
        quantity: 5,
        price: 900,
      },
      {
        product: "chair",
        quantity: 2,
        price: 900,
      },
]

Expected output:

orders = [
      {
        product: "chair",
        quantity: 7,
        price: 900,
      }    
]

Goal: Group object array by product and add the quantity.

  • 1
    what does not work? please add your code. – Nina Scholz Jan 24 '21 at 12:15
  • Are you looking to group just by `product`? What happens when the products are the same but the `price` is different or does that not happen? – Nick Parsons Jan 24 '21 at 12:16
  • yes group by product, and add the quantity. price will be fixed so no problem. – Jonathan Chiong Jan 24 '21 at 12:18
  • What should happen if the product is same and price is different? – Thomas Sablik Jan 24 '21 at 12:22
  • Question needs to be more clear, Is this the only data structure you can use, if so you will need for loop inside for loop, if not you can use a map like data structure. There are many ways, what are your restrictions? – Dogukan Zengin Jan 24 '21 at 12:23
  • I’m voting to close this question because it doesn't show any research or effort. – Thomas Sablik Jan 24 '21 at 12:23
  • Does this answer your question? [how to group by and sum array of object?](https://stackoverflow.com/questions/29364262/how-to-group-by-and-sum-array-of-object) – pilchard Jan 24 '21 at 12:23
  • this will be used in a small app's order section. When user click "add more product", the current value of form will be pushed to this order array. When user click submit, i want to check all the items in order array and make sure the order does not exceed the total quantity of the product. – Jonathan Chiong Jan 24 '21 at 12:28

3 Answers3

2

Here is one of the performant way to achieve that:

var orders = [
  {
    product: "chair",
    quantity: 5,
    price: 900,
  },
  {
    product: "chair",
    quantity: 2,
    price: 900,
  },
];

const resultTest = {};
const result = [];

orders.forEach((item) => {
  if (resultTest[item.product]) {
    const index = resultTest[item.product] -1;
    const foundItem = result[index];
    const newValue = {
      ...foundItem,
      quantity: foundItem.quantity + item.quantity,
    };

    result[index] = newValue;
  } else {
    resultTest[item.product] = result.length + 1;
    result.push(item);
  }
});

console.log(result);
Taghi Khavari
  • 6,272
  • 3
  • 15
  • 32
1

I think you can do this like ths

  var orders = [
      {
        product: "chair",
        quantity: 5,
        price: 900,
      },
      {
        product: "chair",
        quantity: 2,
        price: 900,
      },
]
var output=new Array;
orders.forEach(elem=>{
  var found =false;
  for(var i =0; i<output.length;i++)
  {
    if(output[i].product==elem.product)
    {
      output[i].quantity+=elem.quantity;
      var found = true;
      break;
    }
  }
  if(!found)
  {
    output.push(elem);
  }
})
console.log(output);
Sato Takeru
  • 1,669
  • 4
  • 12
  • 27
0

Here is my solution in more cleaner way:

var orders = [
    {
      product: "chair",
      quantity: 5,
      price: 900,
    },
    {
      product: "chair",
      quantity: 2,
      price: 900,
    },
    {
      product: "table",
      quantity: 1,
      price: 1000,
    }
]

const combineSimilarOrders = {};

orders.forEach((order) => {
    if (!(order.product in combineSimilarOrders)) {
        combineSimilarOrders[order.product] = [ { ...order } ]
    }
    else {
        if (order.product in combineSimilarOrders) {
            let product = Object.keys(combineSimilarOrders);
            
            combineSimilarOrders[product][0].quantity += order.quantity;
            combineSimilarOrders[product][0].price += order.price;
        }
        else {
            combineSimilarOrders[order.product].push(order);
        }
    }
});

console.log(combineSimilarOrders);

Output:

{
  chair: [ { product: 'chair', quantity: 7, price: 1800 } ],
  table: [ { product: 'table', quantity: 1, price: 1000 } ]
}
Harshit
  • 1,510
  • 19
  • 42