Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript在其他函数中减少_Javascript_Json_Reduce - Fatal编程技术网

Javascript在其他函数中减少

Javascript在其他函数中减少,javascript,json,reduce,Javascript,Json,Reduce,如何从JSON文件中获取解析的数据,并通过reduce函数消除重复数据,然后通过调用getfiilteredata()函数可用 async function getFilteredData() { return new Promise((resolve) => { oWebViewInterface.on("loadData", function (data) { var schwellWerte = data

如何从JSON文件中获取解析的数据,并通过reduce函数消除重复数据,然后通过调用
getfiilteredata()
函数可用

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:无法读取未定义的属性“0”,但第一个可以正常工作并记录预期值。

最简单的方法是使用
承诺和异步/等待。将异步调用包装在承诺中,并在客户端等待:

async function getFilteredData() {
    return new Promise( resolve => {
        oWebViewInterface.on("loadData", function (data) {
          var monitorData = JSON.parse(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
        });
    });
}
然后当你打电话时,只需等待电话

var filteredData = await getFilteredData();
console.log(filteredData[0].id);

编辑:我从你的评论中注意到,在你的代码中,你调用了两次
getFilteredata
——这似乎是个坏主意。叫它一次。如果将图表的配置放入自己的
async
方法中,这会变得更容易

async function configChart(){
      var data = await getFilteredData();
      var werteArr = [];
      var zsArr = [];
      for (i = 0; i < data.length; i++) {
         werteArr.push(data[i].wert);
         zsArr.push(data[i].zeitstempel);
      }
        

      //defining config for chart.js
      var config = {
        type: "line",
        data: {
          labels: zsArr ,
          datasets: {
            data: werteArr,
            // backgroundcolor: rgba(182,192,15,1),
          },
        },
        // -- snip rest of config -- //
     }
     var ctx = document.getElementById("canvas").getContext("2d");
     window.line_chart = new window.Chart(ctx, config);
}

window.onload = function () {
    configChart(); // no need to await this. It'll happen asynchronously
};
异步函数configChart(){ var data=await getFilteredata(); var-werteArr=[]; var zsArr=[]; 对于(i=0;i
btw,为什么不使用
filter
而不是
reduce
?什么是
oWebViewInterface
?它只是一个nsWebViewInterface,因为这是来自NativeScript移动应用程序的代码我尝试过你的解决方案,但我无法制作顶级等待。。。“Uncaught SyntaxError:await仅在异步函数中有效”@CRoNiC您只需要使顶级函数
异步
tooI尝试过这样做,但由于我仍然遇到一些无法解决的问题,也许您有时间查看完整的代码:您的代码工作正常,但我再次需要您的帮助,因为我意识到我不完全理解你的代码。。。我需要返回比以前更多的数据-请参阅更新的代码。我想我可以在写注释后自己解决它,但我没有做到这一点