While attempting to filter unique dates, I've noticed that the output varies. In some cases, it works correctly, while in others, it does not. I'm having trouble figuring out what's causing this inconsistency. The filtering method below generally works well, but occasionally fails.
The goal is to extract all unique dates from both the RatingsDaily["2"]
and RatingsDaily["4"]
arrays.
// Data example:
const RatingsDaily = {
"2": [
{"2021-12-24": 3.21},
{"2021-12-25": 3.19},
{"2021-12-28": 3.29},
{"2021-12-29": 3.24},
{"2021-12-30": 3.38},
],
"4": [
{"2021-12-24": 1.0},
{"2021-12-25": 1.0},
{"2021-12-26": 1.0},
{"2022-01-27": 2.0},
{"2022-01-03": 5.0},
{"2022-01-05": 1.0},
]
}
// Current implementation:
let labels = []
let uniquelabel = []
for (let i = 0; i < RatingsDaily["2"].length; i++) {
uniquelabel.push(Object.keys(RatingsDaily["2"][i]));
}
for (let i = 0; i < RatingsDaily["4"].length; i++) {
uniquelabel.push(Object.keys(RatingsDaily["4"][i]));
}
const useFilter = arr => {
return arr.filter((value, index, self) => {
return self.indexOf(value) === index;
});
};
const result = useFilter(uniquelabel);
console.log(result)
I would greatly appreciate any guidance on how to resolve this issue. Thank you for your assistance.