[
{"score": 99, "group":"1"},
{"score": 90, "group":"2"},
{"score":"10", "group":"1"},
{"score":"10", "group":"2"},
]
I work with TypeOrm now and want to filter data.
Expected array:
[
{"score": 99, "group":"1"},
{"score": 90, "group":"2"},
]
const data = [
{"score": 99, "group":"1"},
{"score": 90, "group":"2"},
{"score": 10, "group":"1"},
{"score": 10, "group":"2"}]
const result = [...new Set(data.map(i=>i.group))] // get unique group ids
.map(g=>data.filter(({group})=>g===group) // find members of each group
.sort(({score:a},{score:b})=>a-b).pop()) // find the one with highest score
console.log(result)
If your data includes scores that are strings instead of numbers, change a-b
to (+a)-(+b)
to coerce the strings to numbers.