简体   繁体   中英

nested json by underscore.js

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM