You have two tasks, filtering, and finding distinct items (practically: grouping).
So when we define two functions, one for filtering by a given property:
const filterByProp =
(prop) =>
(value) =>
(items) =>
items.filter((item) => item[prop] === value);
and one for finding the distinct (in this case: each first item per value) by a given property:
const distinctByProp =
(prop) =>
(items) =>
Object.values(items.reduce((group, item) => {
if (!group.hasOwnProperty(item[prop])) group[item[prop]] = item;
return group;
}, {}));
we can do
const onlyClassC = filterByProp("Class")("Class C");
const distinctByGroup = distinctByProp("Group");
const filtered = distinctByGroup(onlyClassC(result));
and get filtered as:
[
{
"Class": "Class C",
"Group": "Group A",
"Value": 1
},
{
"Class": "Class C",
"Group": "Group B",
"Value": 10
}
]
If you want the last item per distinct group instead of the first, remove the if (!group.hasOwnProperty(item[prop])) from distinctByProp.
const filterByProp =
(prop) =>
(value) =>
(items) =>
items.filter((item) => item[prop] === value);
const distinctByProp =
(prop) =>
(items) =>
Object.values(items.reduce((group, item) => {
if (!group.hasOwnProperty(item[prop])) group[item[prop]] = item;
return group;
}, {}));
// -----------------------------------------------------------------------
const onlyClassC = filterByProp("Class")("Class C");
const distinctByGroup = distinctByProp("Group");
// -----------------------------------------------------------------------
var result = [
{
"Class": "Class C",
"Group": "Group A",
"Value": 1
},
{
"Class": "Class C",
"Group": "Group A",
"Value": 2
},
{
"Class": "Class A",
"Group": "Group B",
"Value": 2
},
{
"Class": "Class C",
"Group": "Group B",
"Value": 10
}
];
const filtered = distinctByGroup(onlyClassC(result));
console.log(filtered);