How to group json object dates by month?

3705 views javascript
6

I have a JSON object like so:

{  
   "Sat Jul 28 2018 03:36:36 GMT-0700 (Pacific Daylight Time)":[  
      { ... },
      { ... }
   ],
   "Fri Aug 03 2018 19:07:14 GMT-0700 (Pacific Daylight Time)":[  
      { ... }
   ],
   "Sat Aug 18 2018 17:25:50 GMT-0700 (Pacific Daylight Time)":[  
      { ... }
   ]
}

How can I return this into a grouped object by month?

answered question

seems similar to stackoverflow.com/questions/20630676/… look at the answer of Matt Fletcher

1 Answer

5

Try this (with lodash):

var obj = {  
   "Sat Jul 28 2018 03:36:36 GMT-0700 (Pacific Daylight Time)":[  
      "test", "tester"
   ],
   "Fri Aug 03 2018 19:07:14 GMT-0700 (Pacific Daylight Time)":[  
      "tester1"
   ],
   "Sat Aug 18 2018 17:25:50 GMT-0700 (Pacific Daylight Time)":[  
       "tester2"
   ]
}

var result = _.groupBy(Object.keys(obj), function(key) { return key.substring(4, 7) });

posted this

Have an answer?

JD

Please login first before posting an answer.