Pratap Sharma
Pratap Sharma

Reputation: 2743

What is the best way to get weekly and monthly data from an array of dates?

I have to display a daily/weekly/monthly count of registered users in graph. This is the data what I'm getting from backend. Now I need to loop through the array based on the view type. I have data like this:

[
  {
    "date": "2019-10-28",
    "count": 0
  },
  {
    "date": "2019-10-29",
    "count": 4
  },
  {
    "date": "2019-10-30",
    "count": 5
  },
  {
    "date": "2019-10-31",
    "count": 2
  },
  {
    "date": "2019-11-01",
    "count": 0
  },
  {
    "date": "2019-11-02",
    "count": 6
  },
  {
    "date": "2019-11-03",
    "count": 0
  },
  {
    "date": "2019-11-04",
    "count": 0
  },
  {
    "date": "2019-11-05",
    "count": 0
  },
  {
    "date": "2019-11-06",
    "count": 0
  },
  {
    "date": "2019-11-07",
    "count": 0
  },
  {
    "date": "2019-11-08",
    "count": 0
  },
  {
    "date": "2019-11-09",
    "count": 0
  }
]

If weekly view type then I need to get result something like this:

[
  {
    "week": 44,
    "count": 15
  },
  {
    "week": 45,
    "count": 13
  },
  {
    "week": 46,
    "count": 3
  },
  {
    "week": 47,
    "count": 13
  }
]

If montly then i need to get result something like this:

[
  {
    "10": 100,
    "count": 15
  },
  {
    "11": 45,
    "count": 13
  },
  {
    "12": 460,
    "count": 3
  }
]

Using a library moment I am able to get the week number like this:

array.forEach((item, index) => {
   var weeknumber = moment(item.date).week();
   console.log("Week ", weeknumber);
})

I'm not sure about the best way and best approch to get the desired result. Any suggestion? Thanks!

Upvotes: 3

Views: 1196

Answers (1)

abhirathore2006
abhirathore2006

Reputation: 3745

you need to process the data and prepare a common logic to transform the data into correct format.

you can use groupBy and forEach along with reduce method to prepare data something like below example

Demo: https://repl.it/@abhirathore2006/PrepareDataForCharts

const _ = require('lodash');
// use your dataset
let data = [
  {
    "date": "2019-10-28",
    "count": 0
  },
  {
    "date": "2019-10-29",
    "count": 4
  }
];

// prepare data beforehand to save re-calculations
data.forEach(d=>{
  d.dateObj = new Date(d.date)
});

// this method will sum all the counts in given group
function buildData(data, keyName ){
  let result = []; 
  _.forEach(data, (val, key)=>{
    let totalCounts  = val.reduce((acc, curr)=>{
        return acc + curr.count;
    }, 0)
    result.push({[keyName]:key, count:totalCounts})
  })
  return result;
}

// this will group data by given output of date method
function groupAndBuild(data, dateMethod, groupKey) {
  let groupedData = _.groupBy(data, (d)=>{
    return d.dateObj[dateMethod]()
  })
  return buildData(groupedData, groupKey)
}

// use moment-js or other library to get week number
// and replace dateObj with moment object
console.log(groupAndBuild(data,'getMonth','month'))

Upvotes: 2

Related Questions