I'm facing a rather complex scenario where I'm looking for a way to manipulate data using lodash and moment. Let's say I have a date range and the following initial data:
var startDate = "2018-02-21"
var endDate = "2018-02-23"
var data = [
{
"date": "2018-02-21 21:21:17",
"group": "A"
},
{
"date": "2018-02-21 21:21:17",
"group": "B"
},
{
"date": "2018-02-23 21:21:17",
"group": "A"
},
{
"date": "2018-02-21 21:21:17",
"group": "B"
}
];
My goal is to utilize lodash to group all the "group" fields and create a new field in the resulting object called "dates". This new field will consist of key/value pairs representing dates, where the keys will be within the date range (from startDate to endDate) and the values will indicate the count of matching dates.
The desired output format would be as follows:
var output = [
{
"group": "A",
"dates": [
"2018-02-21": 1,
"2018-02-22": 0
"2018-02-23": 1
]
},
{
"group": "B",
"dates": [
"2018-02-21": 2,
"2018-02-22": 0,
"2018-02-23": 0
]
}
];
I have set up a jsfiddle with moment and lodash included to illustrate this scenario.
http://jsfiddle.net/dp7rzmw5/6467/
Your assistance with this matter would be greatly appreciated!