Reputation: 2314
i have found these related posts but not able to solve my issue
Underscore, Nested Group By and Generate a JSON
Grouping a nested array with underscore.js
I want to group like following :
like StartDate ---> FunctionID--> STartTime
this is my query result
and json result is like following :
[{"Act_Qty":0,"FunctionID":268,"ResDesc":"Anniversary Party","StartTime_EndTime":"04:00:00 AM - 04:30:00 AM"},
[{"Act_Qty":0,"FunctionID":268,"ResDesc":"Anniversary Party","StartTime_EndTime":"04:00:00 AM - 04:30:00 AM"}]
for nested json grouping I Tried following different queries :
var result = _.chain(jsonData)
.groupBy('StartDate')
.mapObject( StartDate => _.groupBy(StartDate, 'FunctionID'))
.value();
var result = _.chain(jsonData)
.groupBy('FunctionID')
.mapObject( FunctionID => _.groupBy(FunctionID,'StartDate' ))
.value();
but this is giving me results like
and
like same functions in inner group ,But i want different functions in inner group
please suggest
Upvotes: 1
Views: 153
Reputation: 1546
This is how you can nested group data.
data = [{
"StartDate": "2018-09-11",
"FunctionID": "276",
"StartTime_EndTime": "08:00:00 AM - 11:00:00 AM",
"ResDesc": "Breakfast",
"functionRoom": "Living Room",
"Gurenteed": "13",
"Expr": "12",
"Act_Qty": "13",
"Charge": "19",
"Item": "Tea Sandwitches and Salads"
}, {
"StartDate": "2018-09-11",
"FunctionID": "276",
"StartTime_EndTime": "08:00:00 AM - 11:00:00 AM",
"ResDesc": "Breakfast",
"functionRoom": "Living Room",
"Gurenteed": "13",
"Expr": "12",
"Act_Qty": "13",
"Charge": "18",
"Item": "Pasta Station"
}, {
"StartDate": "2018-09-12",
"FunctionID": "295",
"StartTime_EndTime": "07:00:00 AM - 07:30:00 AM",
"ResDesc": "Breakfast",
"functionRoom": "Living Room",
"Gurenteed": "13",
"Expr": "12",
"Act_Qty": "13",
"Charge": "1",
"Item": "Tea Sandwitches and Salads"
}, {
"StartDate": "2018-09-12",
"FunctionID": "295",
"StartTime_EndTime": "07:00:00 AM - 07:30:00 AM",
"ResDesc": "Breakfast",
"functionRoom": "Living Room",
"Gurenteed": "26",
"Expr": "18",
"Act_Qty": "19",
"Charge": "9",
"Item": "Coffee"
}, {
"StartDate": "2018-09-13",
"FunctionID": "298",
"StartTime_EndTime": "09:00:00 AM - 11:00:00 AM",
"ResDesc": "Breakfast",
"functionRoom": "Living Room",
"Gurenteed": "13",
"Expr": "12",
"Act_Qty": "13",
"Charge": "19",
"Item": "Tea Sandwitches and Salads"
}, {
"StartDate": "2018-09-13",
"FunctionID": "298",
"StartTime_EndTime": "07:00:00 AM - 11:00:00 AM",
"ResDesc": "Breakfast",
"functionRoom": "Living Room",
"Gurenteed": "13",
"Expr": "12",
"Act_Qty": "13",
"Charge": "18",
"Item": "Pasta Station"
}, {
"StartDate": "2018-09-15",
"FunctionID": "299",
"StartTime_EndTime": "06:00:00 AM - 07:30:00 AM",
"ResDesc": "Breakfast",
"functionRoom": "Living Room",
"Gurenteed": "13",
"Expr": "12",
"Act_Qty": "13",
"Charge": "1",
"Item": "Tea Sandwitches and Salads"
}, {
"StartDate": "2018-09-16",
"FunctionID": "299",
"StartTime_EndTime": "06:30:00 AM - 07:30:00 AM",
"ResDesc": "Breakfast",
"functionRoom": "Living Room",
"Gurenteed": "26",
"Expr": "18",
"Act_Qty": "19",
"Charge": "9",
"Item": "Coffee"
}];
var byStartDate = _.groupBy(data, 'StartDate');
_.each(byStartDate, (d, i) => {
byStartDate[i] = _.groupBy(d, 'FunctionID');
_.each(byStartDate[i], (d1, i1) => {
byStartDate[i][i1] = _.groupBy(d1, d2 => {
return d2["StartTime_EndTime"].split("-")[0].trim();
});
});
});
console.log(byStartDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
let me know if any concern.
Upvotes: 1