简体   繁体   中英

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

{
 "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

February 1

March 1

April 2

I'm using Node and 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:

servicedata.responses.forEach(obj => obj.month = obj.date.slice(5, 7));

and then group by it :

{% for month, items in servicedata.responses | groupby("month") %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM