【问题标题】:How to group and count JSON data by multiple keys and for the last 30 days in Javascript/jQuery如何在 Javascript/jQuery 中通过多个键和过去 30 天对 JSON 数据进行分组和计数
【发布时间】:2019-08-22 09:47:18
【问题描述】:

我正在显示一个图表,我需要知道如何从数组中提取我需要的特定数据。

我正在尝试创建一个基于三列的 JSON 数组图表。来源、活动和日期。对它们进行分组、计数,然后将它们显示在图表中。这似乎比我想象的要棘手,因为我得到的结果是 NaN 并且与我想要成功的结果完全不同。 这是我拥有的 JSON 数组

results = [{
    "utm-source": "direct",
    "utm-campaign": "nothing",
    "Date": "2019\/05\/10"
},
{
    "utm-source": "direct",
    "utm-campaign": "nothing",
    "Date": "2019\/08\/08"
},
{
    "utm-source": "direct",
    "utm-campaign": "nothing",
    "Date": "2019\/08\/09"
},
{
    "utm-source": "direct",
    "utm-campaign": "nothing",
    "Date": "2019\/08\/12"
},
{
    "utm-source": "google",
    "utm-campaign": "spring_sale",
    "Date": "2019\/08\/21"
},
{
    "utm-source": "facebook",
    "utm-campaign": "spring_sale",
    "Date": "2019\/08\/21"
},
{
    "utm-source": "email",
    "utm-campaign": "spring_sale",
    "Date": "2019\/08\/21"
}]

我已经尝试过了,但离我想要的结果还很远:

var today = new Date();

var year = today.getFullYear();
var month = today.getMonth();
var date = today.getDate();

// build the last 30 days date array
var last30days = [];
for(var i=0; i<30; i++){
   var day = new Date(year, month, date - i);
   day = day.toISOString().split("T")[0].replace(/-/g,"/")
   last30days.push(day);
}
console.log(last30days);

var finalArray = [];
last30days.forEach(function(day,dayIndex){
   results.forEach(function(result,resultIndex) {
       var found = result.Date.indexOf(day);
       console.log(found);
       if(found == 0) {
           //console.log(result);
           finalArray[result.utm_source] += result.Lead_Source;
       } else {
           finalArray[result.utm_source] += "0";
       }
   });
});

预期的结果应该是这样的:https://imgur.com/a/eoCl8rW 将此数组与最近 30 天的另一个数组进行比较,并显示返回的潜在客户数量(直接无或 facebook spring_sale)以及没有潜在客户的日子返回 0 并将其显示到图表中以获取等效的活动和来源只显示一次。 此外,预期的数组应该是这样的:

{
    "direct": {
        "nothing": {
            "2019/07/24" : "0",
            "2019/07/25" : "0",
            "2019/07/26" : "0",
            "2019/07/27" : "0",
            "2019/07/28" : "0",
            "2019/07/29" : "0",
            "2019/07/30" : "0",
            "2019/07/31" : "0",
            "2019/08/01" : "0",
            "2019/08/02" : "0",
            "2019/08/03" : "0",
            "2019/08/04" : "0",
            "2019/08/05" : "0",
            "2019/08/06" : "0",
            "2019/08/07" : "0",
            "2019/08/08" : "1",
            "2019/08/09" : "1",
            "2019/08/10" : "0",
            "2019/08/11" : "0",
            "2019/08/12" : "1",
            "2019/08/13" : "0",
            "2019/08/14" : "0",
            "2019/08/15" : "0",
            "2019/08/16" : "0",
            "2019/08/17" : "0",
            "2019/08/18" : "0",
            "2019/08/19" : "0",
            "2019/08/20" : "0",
            "2019/08/21" : "0",
            "2019/08/22" : "0"
        }
    },
    "google":{
        "spring_sale": {
            "2019/07/24" : "0",
            "2019/07/25" : "0",
            "2019/07/26" : "0",
            "2019/07/27" : "0",
            "2019/07/28" : "0",
            "2019/07/29" : "0",
            "2019/07/30" : "0",
            "2019/07/31" : "0",
            "2019/08/01" : "0",
            "2019/08/02" : "0",
            "2019/08/03" : "0",
            "2019/08/04" : "0",
            "2019/08/05" : "0",
            "2019/08/06" : "0",
            "2019/08/07" : "0",
            "2019/08/08" : "0",
            "2019/08/09" : "0",
            "2019/08/10" : "0",
            "2019/08/11" : "0",
            "2019/08/12" : "0",
            "2019/08/13" : "0",
            "2019/08/14" : "0",
            "2019/08/15" : "0",
            "2019/08/16" : "0",
            "2019/08/17" : "0",
            "2019/08/18" : "0",
            "2019/08/19" : "0",
            "2019/08/20" : "0",
            "2019/08/21" : "1",
            "2019/08/22" : "0"
        }
    },
    "facebook":{
        "spring_sale": {
            "2019/07/24" : "0",
            "2019/07/25" : "0",
            "2019/07/26" : "0",
            "2019/07/27" : "0",
            "2019/07/28" : "0",
            "2019/07/29" : "0",
            "2019/07/30" : "0",
            "2019/07/31" : "0",
            "2019/08/01" : "0",
            "2019/08/02" : "0",
            "2019/08/03" : "0",
            "2019/08/04" : "0",
            "2019/08/05" : "0",
            "2019/08/06" : "0",
            "2019/08/07" : "0",
            "2019/08/08" : "0",
            "2019/08/09" : "0",
            "2019/08/10" : "0",
            "2019/08/11" : "0",
            "2019/08/12" : "0",
            "2019/08/13" : "0",
            "2019/08/14" : "0",
            "2019/08/15" : "0",
            "2019/08/16" : "0",
            "2019/08/17" : "0",
            "2019/08/18" : "0",
            "2019/08/19" : "0",
            "2019/08/20" : "0",
            "2019/08/21" : "1",
            "2019/08/22" : "0"
        }
    }
}

【问题讨论】:

  • 发布您尝试的代码以及预期的输出数组。
  • 抱歉刚刚添加了!谢谢!

标签: javascript arrays json multidimensional-array grouping


【解决方案1】:

如果您只需要按utm-source 分组,您可以这样做

const counts = {};
results.forEach(r => {
  counts[r["utm-source"]] = (counts[r["utm-source"]] || 0) + 1;
});

输出看起来像

{ direct: 4, google: 1, facebook: 1, email: 1 } 

类似于How to count duplicate value in an array in javascript,但使用对象而不是字符串数组

【讨论】:

    猜你喜欢
    • 2022-11-14
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2018-02-20
    • 2016-04-30
    相关资源
    最近更新 更多