I have an array of objects(shopping cart) and I want to remove duplicates that have same obj.id
and obj.arr
an empty array.After that to increase obj.quantity
by one.
Here is the original arr:
const cart = [
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
},
{
"id": 2,
"name": "pen",
"arr": ["asa", "asd"],
"quantity": 3
},
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
},
{
"id": 3,
"name": "ball pen",
"arr": ["azx", "dcv"],
"quantity": 1
}
]
Expected output should be like this:
const cart = [
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 2
},
{
"id": 2,
"name": "pen",
"arr": ["asa", "asd"],
"quantity": 3
},
{
"id": 3,
"name": "ball pen",
"arr": ["azx", "dcv"],
"quantity": 1
}
]
I don't know how to update object.quantity
after removing the duplicate. And if possible I want to keep the item with smallest index.
Could you please help me?
Thank you