The array contains a list with products that are consecutive duplicated. I need to remove the consecutive duplicate products in the array and sum their quantities.
In this list of objects we have four values of (D)
and two values of (U)
. If the values are repeated and consecutive, I want to remove prev value and sum qty in last duplicate value.
let books = [
{ id: "1", type: "D", price: 25, qty: 27},// *
{ id: "2", type: "D", price: 22, qty: 75},// *
{ id: "3", type: "D", price: 21, qty: 19},// *
{ id: "4", type: "D", price: 19, qty: 62},
{ id: "5", type: "U", price: 23, qty: 19},
{ id: "6", type: "D", price: 20, qty: 22},
{ id: "7", type: "U", price: 25, qty: 14},// *
{ id: "8", type: "U", price: 29, qty: 14}
]
The result will be like this:
[
{ id: "4", type: "D", price: 19, qty: 121},
{ id: "5", type: "U", price: 23, qty: 19},
{ id: "6", type: "D", price: 20, qty: 22},
{ id: "8", type: "U", price: 29, qty: 28}
]
and Thank you in advance.