Looking for help with this array:
array = [{id: 1, name: 'apple'}, {id: 2, name: 'banana'}, {id: 3, name:
'apple'}]
I want to eliminate objects with duplicated "name" property while retaining the highest id for each unique object, resulting in:
newarray = [ {id: 2, name: 'banana'}, {id: 3, name: apple}]
This is what I have attempted so far:
array = [{id: 1, name: 'apple'}, {id: 2, name: 'banana'}, {id: 3, name:
apple}]
newarray = Array.from(new Set(array.map(x => x.id)))
.map(id => {
return {
id: id,
name: array.find( s => s.id === id).name
})
The current result obtained is:
newarray = [ {id: 2, name: 'banana'}, {id: 1, name: apple}]
While duplicate objects are removed, the issue lies in not getting the highest id for each remaining object.
Appreciate any suggestions on how to achieve the desired outcome. Thank you!