I have an array like this
const allChampions =
[ { champion: 'Vayne', percentage: 33.33, role: 'top' }
, { champion: 'Jinx', percentage: 0, role: 'jungle' }
, { champion: 'Vayne', percentage: 0, role: 'mid' }
, { champion: 'Jinx', percentage: 100, role: 'bottom' }
, { champion: 'Vayne', percentage: 0, role: 'support' }
]
I want to filter out the array by checking for duplicate champions. If the champion is a duplicate, I want to remove the champion with the lowest percentage.
I've tried filtering the array into a new one using this:
let sortedChampions = [];
sortedChampionsWithDuplicates.map((x) =>
sortedChampions.filter((a) => a.champion == x.champion && a.percentage >= x.percentage).length > 0 ? null : sortedChampions.push(x)
);
But this is what the end result ends up being:
[{"champion":"Vayne","percentage":33.33,"role":"top"},{"champion":"Jinx","percentage":0,"role":"jungle"},{"champion":"Jinx","percentage":100,"role":"bottom"}]