[英]How can I group JSON data and count by month element using Nunjucks or Node.JS
Given this data as an example, this is just 2 records, there's hundreds over several months 以这些数据为例,这只是2条记录,几个月就有数百条记录
{
"responses": [{
"responseid": 1,
"q1": 1,
"q2": 1,
"q3": 1,
"q4": 1,
"q5": 2,
"response": "Response 1 example feedback",
"date": "2018-02-12T00:00:00"
},
{
"responseid": 2,
"q1": 1,
"q2": 2,
"q3": 1,
"q4": 1,
"q5": 1,
"response": "Response 2 example feedback",
"date": "2018-03-15T00:00:00"
},
{
"responseid": 3,
"q1": 1,
"q2": 2,
"q3": 1,
"q4": 1,
"q5": 1,
"response": "Response 3 example feedback",
"date": "2018-04-15T00:00:00"
},
{
"responseid": 4,
"q1": 1,
"q2": 2,
"q3": 1,
"q4": 1,
"q5": 1,
"response": "Response 4 example feedback",
"date": "2018-04-15T00:00:00"
}]
}
How do I group the data by the month part so that I can display a count of records in any given month. 如何按月份部分对数据进行分组,以便我可以显示任何给定月份的记录数。 So the 4 responses above, 2 are from April, 1 each from Feb and March
所以上面的4个回复,2个来自4月,1个来自2月和3月
February 1 2月1日
March 1 3月1日
April 2 4月2日
I'm using Node and Nunjucks. 我正在使用Node和Nunjucks。
Thought I'd add, I can group the records by a date as a starting point. 我想补充一点,我可以将日期分组作为起点。
{% for date, items in servicedata.responses | groupby("date") %}
<b>{{ date }}</b> :
{{items.length}}<br>
{% endfor %}
Simple count: 简单计数:
const moment = require('moment');
let res = {};
data.responses.forEach(item => {
let month = moment(item.date).startOf('month');
res[month] = res[month] || 0;
res[month]++;
});
console.log(res);
/* 'Thu Feb 01 2018 00:00:00 GMT+0300': 1,
'Thu Mar 01 2018 00:00:00 GMT+0300': 1,
'Sun Apr 01 2018 00:00:00 GMT+0300': 2 */
Grouping: 分组:
const moment = require('moment');
let res = {};
data.responses.forEach(item => {
let month = moment(item.date).startOf('month');
res[month] = res[month] || { count: 0, responses: [] };
res[month].count++;
res[month].responses.push(item);
});
console.log(res)
This might not be the cleanest solution, but it'll do the job. 这可能不是最干净的解决方案,但它会完成这项工作。
var yourJson = {"responses":[{"date":"2018-02-12T00:00:00"},{"date":"2018-03-15T00:00:00"},{"date":"2018-04-15T00:00:00"},{"date":"2018-04-15T00:00:00"}]}; function mix() { var counts={}; yourJson.responses.forEach(element => { var mydate = (new Date(element.date)).toLocaleString("en-us", { month: "long" }); counts[mydate] = counts[mydate] ? ++counts[mydate] : 1; }); for (var key in counts) { document.write(key + ' ' + counts[key] + '<br>'); } } mix();
Similar but without having to import moment. 类似但无需导入时刻。 First prepare the result object then loop over all objects in the response and look for the month.
首先准备结果对象,然后遍历响应中的所有对象并查找月份。
res={'01':0,'02':0,'03':0,'04':0,'05':0,'06':0,'07':0,'08':0,'09':0,'10':0,'11':0,'12':0,}
data.responses.forEach(obj=>{
res[obj.date.split('-')[1]]+=1
})
console.log(res)
Doesn't seem like groupby supports custom expressions, but you can add the month using JavaScript: 似乎groupby不支持自定义表达式,但您可以使用JavaScript添加月份:
servicedata.responses.forEach(obj => obj.month = obj.date.slice(5, 7));
and then group by it : 然后分组:
{% for month, items in servicedata.responses | groupby("month") %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.