score:1

Accepted answer

it is worth adding some more data, but here you go.

let rawdata = [
  {
    "clicks": {
        "2019-01": [
            {
                "clicks": "47",
                "id": 63,
                "type": 0,
                "user": 5
            },
            {
                "clicks": "459",
                "id": 5,
                "type": 0,
                "user": 5
            }
       ],
        "2019-02": [
            {
                "clicks": "0",
                "id": 44,
                "type": 0,
                "user": 12
            }
         ]
      }
   }
];

const mymonthlyclickcount = (record) => {
  if(array.isarray(record) === false) record = [record];
  return record.map(year => {
    let arrmonth = [];
    for(mth in year.clicks) {
      let m = {
        month: mth,
        clicks: year.clicks[mth].reduce((a,v) => a+=parseint(v.clicks),0)
      };
      arrmonth.push(m);
    }
    return arrmonth;
  });
};



console.log(mymonthlyclickcount(rawdata));
console.log(mymonthlyclickcount(rawdata[0]));


Related Query

More Query from same tag