Есть много способов сделать это. Один будет:
function filterLowestCounts(a) {
const lowestCountsByCategory = a.reduce(
(result, { count, category }) => ({
...result,
[category]: Math.min(count, result[category] || Number.MAX_SAFE_INTEGER)
}),
{}
)
return Object.entries(lowestCountsByCategory).map(
([category, count]) => ({ count, category: Number(category) })
)
}
Назовите это так:
filterLowestCounts([
{count: 1, category: 4},
{count: 2, category: 4},
{count: 3, category: 2},
{count: 4, category: 2},
{count: 5, category: 8},
{count: 6, category: 8},
{count: 7, category: 1},
{count: 8, category: 1},
{count: 9, category: 1},
{count: 10, category: 8},
])
Результат:
[
{count: 7, category: 1},
{count: 3, category: 2},
{count: 1, category: 4},
{count: 5, category: 8},
]