Create one new array from given array and get different properties as a Child object/Array

I have Responses array with multiple id but different question answers.

Responses = 
[0:{Id : 1,Name : John, QuestionId :1,Answer :8},
1:{Id : 1,Name : John, QuestionId :2,Answer :9},
2:{Id : 1,Name : John, QuestionId :3,Answer :3},
3:{Id : 1,Name : John, QuestionId :4,Answer :2},
4:{Id : 2,Name : Mark, QuestionId :1,Answer :6},
5:{Id : 2,Name : Mark, QuestionId :2,Answer :2},
6:{Id : 2,Name : Mark, QuestionId :3,Answer :5},
7:{Id : 2,Name : Mark, QuestionId :4,Answer :1}]
----------------------------------------------------------------
Output I want :
 
Arr = 
[0:{Id : 1,Name : John,
 Question : [{QuestionId :1,Answer :9},{QuestionId :2,Answer :8},{QuestionId :3,Answer :3},{QuestionId :4,Answer :2}]},
1:{Id : 2,Name : Mark,
 Question : [{QuestionId :1,Answer :6},{QuestionId :2,Answer :2},{QuestionId :3,Answer :5},{QuestionId :4,Answer :1}]}]
---------------------------------------------------------

I want to create one array with Id, Name and one Question object which would have Question Id with answer. I tried different way but that didn’t work for me. It would be great if you can help me here. Thank you.

20 thoughts on “Create one new array from given array and get different properties as a Child object/Array”

Leave a Comment