1

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 :

enter image description here

like StartDate ---> FunctionID--> STartTime

this is my query result

enter image description here

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

enter image description here

and

enter image description here

like same functions in inner group ,But i want different functions in inner group

please suggest

6
  • 1
    plz provide some input sample and expected output format. Commented Sep 24, 2018 at 5:50
  • 1
    JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented Sep 24, 2018 at 5:53
  • 1
    "...and json result is like following..." That isn't JSON. Commented Sep 24, 2018 at 5:54
  • @NeerajVerma - "I already did everything" No, Aagam's comment requested an input sample and expected output format. There's no "input sample" in the question that I can see. And a picture of table isn't "output format" (it's a rendering of the output format). If you want people to help you, you need to provide them with clear, complete information. Commented Sep 24, 2018 at 5:55
  • @NeerajVerma can you plz provide json itself as It is hard to get data from screenshot. Commented Sep 24, 2018 at 5:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.