Is there a way to filter out duplicates from the data parsed from my JSON file using the reduce function and make it accessible by calling the getFilteredData()
function?
async function getFilteredData() {
return new Promise((resolve) => {
oWebViewInterface.on("loadData", function (data) {
var thresholdValues = data.monitor;
var filteredData = data.data.reduce((arr, d) => {
if (arr.find((i) => i.timestamp === d.timestamp)) {
return arr;
} else {
return [...arr, d];
}
}, []);
resolve(filteredData); // resolve the promise with the filtered data
//can I do: resolve(filteredData, thresholdValues) to resolve both?
});
});
}
However, implementing this approach leads to an error message "Uncaught TypeError: Cannot read property '0' of undefined" for the last two console.log()
, while the first one works correctly and logs the expected value.