Jason Jason - 1 year ago 267
Javascript Question

Group by Week using Angular, Underscore and Moment js

Need some help please. I am trying to group my click stats by week using underscore and moment.

here is the code:

var groupedByWeekFbCompleted = _.groupBy($scope.facebookObjects, function(item) {
return moment(item.timeclicked,"YYYY-MM-DD").isoWeek();

here is a screen shot of the chart it gives me
enter image description here

When I print groupedByWeekFbCompleted to the console this is what I get
enter image description here

The data comes from the DB formatted like this

2016-06-18 14:03:56

I can not figure out what 24, 26, 27, 28 and 29 represent. My hope is to display the first day of the week as a label and then group by that week

Answer Source

If you will take that date string and parse it using moment you will get something like

moment("2016-11-08 14:03:56", "YYYY-MM-DD HH:mm:ss").isoWeek()
// 45

24, 26, 27, 28 and 29 are the week day, and the values are arrays of dates within that week


To display a date string on your labels, you can use the .format method to parse the timestamp you get from the moment function, provide it the same format you used, like so:

moment("2016-11-08 14:03:56", "YYYY-MM-DD HH:mm:ss").format("YYYY-MM-DD HH:mm:ss") 
// output: "2016-11-08 14:03:56"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download