Skip to content
Advertisement

Compare objects within an array and create a new array without duplicates

I’ve an array of objects:

JavaScript

I want to compare the objects within an array by name. If the duplicate name exists, I should compare the age and only keep the higher age object.

The expected output should be:

JavaScript

I am new to javascript/typescript and couldn’t find any optimal solution for this problem. I hope, I was able to explain my problem clearly.

Thanks.

Advertisement

Answer

try this

JavaScript

newArr

JavaScript

groupby

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement