I have this scenario where I need to fetch unique values of all objects based on a dynamically passed property . I have tried the following approach but does not seem like working.
var arr = [
{
id: "1",
type: "x",
source: {
val1: "3",
val2: "4",
val3: "6",
},
},
{
id: "1",
type: "x",
source: {
val1: "3",
val2: "4",
val3: "6",
},
},
{
id: "1",
type: "x",
source: {
val1: "4",
val2: "5",
val3: "6",
}
}
];
Now say I pass val1 it should give me unique values 3,4 and if I pass val2 it should give me 4,5. P.S : I will only pass the parameter that are present inside source property.
Approach that I have tried:
calculate = (param) =>
{
let uniqueValues = Array.from(
new Set(arr.map((arr: any) => arr[param]))
);
}