I have getting some data which is somewhat look like this.
var myData = [{Name : "Alex",Roll : 1,Class : 1,Marks :[{Maths: 100,Science : 200}],Weight: 50},
{Name : "Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 40},
{Name : "Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 30},
{Name : "Alex",Roll : 1,Class :1, Marks :[{Maths: 100,Science : 200, }],Weight: 60},
{Name : "Cean",Roll : 3,Class :1, Marks :[{Physic: 100,Economics : 200, }],Weight: 40}]
What I have tried is this code.
function groupBy(xs, f) {
return xs.reduce((r, v, i, a, k = f(v)) => ((r[k] || (r[k] = [])).push(v), r), {});
}
This is grouping the data but I want to store into array. Two keys should be outside and after that remaining stuff should be inside an array under collection.
newData = [{Name : "Alex",
Roll: 1,
collection :[{Name : "Alex",Roll : 1,Class : 1,Marks :[{Maths: 100,Science : 200}],Weight: 50},
{Name : "Alex",Roll : 1,Class :1, Marks :[{Maths: 100,Science : 200, }],Weight: 60}]
},
{Name : "Brat",
Roll: 2,
collection :[{Name : "Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 40},
{"Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 30}]
},
{Name : "Cean",
Roll: 3,
collection :[{Name : "Cean",Roll : 3,Class :1, Marks :[{Physic: 100,Economics : 200, }],Weight: 40}],
}
]
Can anybody help me to modify my code and help meto reach the solution.