removing duplicate objects in an array, keeping ones with maximum property value

Viewed 1975

I have an array like this:

const array=[ {id:0, quantity:1}, {id:1, quantity:2}, {id:0, quantity:4} ]

My GOAL is to be like this:

const array=[ {id:1, quantity:2}, {id:0, quantity:4} ]

The order of the object does not matter as long as it can find the 'id' with the larger quantity

I tried filter + findIndex, map +filter, etc but I kept making mistake. I need help.

4 Answers
Related