如何在 Javascript/jQuery 中按多个键和过去 30 天的 JSON 数据进行分组和计数



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

我正在尝试创建基于三列的 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://i.stack.imgur.com/gbvMT.jpg 将此数组与具有过去 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"
}
}
}

如果你只需要按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 } 

类似于如何在javascript中计算数组中的重复值,但使用对象而不是字符串数组

最新更新