Array Reduce提供不确定的数据结果

时间:2019-10-17 16:58:45

标签: javascript arrays json highcharts axios

我正在尝试根据名称的唯一值来累积JSON值。但是我一直在获取未定义的数据。

我的JSON:

[
    {
        "date": "2019-09-16",
        "browser": "Firefox",
        "hello": 487586,
        "bye": 398217
    },
    {
        "date": "2019-09-16",
        "browser": "Firefox",
        "hello": 2661089,
        "bye": 2111027
    },
    {
        "date": "2019-09-16",
        "browser": "Chrome",
        "hello": 218492,
        "bye": 164685
    },
    {
        "date": "2019-09-16",
        "browser": "IE",
        "hello": 164572,
        "bye": 86309
    },
    {
        "date": "2019-09-16",
        "browser": "Chrome",
        "hello": 67534,
        "bye": 19736
    }
]

我的代码:

let json = `json_url`;
let Social_Media = ['Facebook', 'Instagram', 'Twitter', 'LinkedIn', 'TikTok'];

axios.get(json, {
        cancelToken: new CancelToken(function executor(c) {
          api_requests.json_count = c;
        })
      })
      .then(function(json_counts) {
        let json_chart_data = json_counts.data;

        let val = [...new Set(json_chart_data.map(({
          nm
        }) => nm))];
        console.log(val);

        series = val.reduce((a, c, i) => {
          console.log(a);
          cur_data = json_chart_data.filter(d => {
            return d.nm === c
          }).sort(compare_values('date'));

          return [...a, {
            name: c.replace(/_/g, ' '),
            data: cur_data.reduce((aa, cc) => {
              // console.log(aa);
              return [...aa, {
                x: +date(cc),
                y: +cc.hello,
                bye: +cc.bye
              }];
            }, []),
            Social: Social_Media[i]
          }];
        }, []);

val返回["Firefox","Chrome","IE"]

我希望console.log(a)返回:

[]
[0:{data:[json_data for Firefox]}, name:"Firefox", Social_Media:"Facebook"}]
[0:{data:[json_data for Firefox]}, name:"Firefox", Social_Media:"Facebook"}, 1:{data:[json_data for Chrome]}, name:"Chrome", Social_Media:"Instagram"}]
...

我得到:

[]
[0:{data:undefined}, name:"Firefox", Social_Media:"Facebook"}]
[0:{data:undefined}, name:"Firefox", Social_Media:"Facebook"}, 1:{data:undefined}, name:"Chrome", Social_Media:"Instagram"}]

我在做什么错?我只想合并每种浏览器的所有结果。是因为我要找到独特的浏览器,然后使用reduce方法将它们组合在一起吗?

0 个答案:

没有答案