【发布时间】:2020-10-05 20:06:47
【问题描述】:
如何从我的 JSON 文件中获取我解析的数据以运行 reduce 函数以消除重复项,然后通过调用 getFiilteredData() 函数获得可用的数据?
async function getFilteredData() {
return new Promise((resolve) => {
oWebViewInterface.on("loadData", function (data) {
var schwellWerte = data.monitor;
var monitorData = data.data.reduce((arr, d) => {
if (arr.find((i) => i.zeitstempel === d.zeitstempel)) {
return arr;
} else {
return [...arr, d];
}
}, []);
resolve(monitorData); // resolve the promise with the data
//can I do: resolve(monitorData, schwellWerte) to resolve both?
});
});
}
这样做会导致最后两个 console.log() 出现“Uncaught TypeError: Cannot read property '0' of undefined”,但第一个工作正常并记录预期值。
【问题讨论】:
-
顺便说一句,为什么不使用
filter而不是reduce? -
什么是
oWebViewInterface? -
它只是一个 nsWebViewInterface,因为这是来自 NativeScript 移动应用程序的代码
标签: javascript json reduce