I am inexperienced with lodash, but I believe it can help me transform data into a desired format. I have tried varying level of methods described in the documentation, but I can not wrap my head around everything. I've looked here on SO, a few blogs, and the documentation. I have tried combining groupby and map, but I was unable to resolve the issue. I was also unsure how to log the steps.
Here is what I want to do, I want to take the following array and turn it into the array after it. Can anyone point me in the right direction?
Original Data
var mockData = [
{
"notice_title": "Bad news",
"notice_text": "Server is down!",
"start_date": "2016-09-18T04:00:00Z"
},
{
"notice_title": "Weekly Reminder",
"notice_text": "Please read the assignment!",
"start_date": "2016-09-18T04:00:00Z"
},
{
"notice_title": "Sweet",
"notice_text": "This morning, the new edition of our blog hit stands!",
"start_date": "2016-09-19T04:00:00Z"
},
{
"notice_title": "Yeah",
"notice_text": "This is pretty cool",
"start_date": "2016-09-19T04:00:00Z"
}
Desired Data
var newMockData = [
{
"date": "JAN 18 2016",
"messages": [{
"notice_title": "Bad news",
"notice_text": "Server is down!",
"start_date": "2016-09-18T04:00:00Z"
},
{
"notice_title": "Weekly Reminder",
"notice_text": "Please read the assignment!",
"start_date": "2016-09-18T04:00:00Z"
}],
"date": "JAN 19 2016",
"messages": [{
"notice_title": "Sweet",
"notice_text": "This morning, the new edition of our blog hit stands!",
"start_date": "2016-09-19T04:00:00Z"
},
{
"notice_title": "Yeah",
"notice_text": "This is pretty cool",
"start_date": "2016-09-19T04:00:00Z"
}]
}]
Updated lodash
var result = _.chain(mockData)
.groupBy(function(item) {
return moment(item.start_date.substring(0,10)).format("MMM-DD-YYYY");
})
.map((value, key) => {
return {
date: key,
param: value
}
})
.value();