hello all i am facing some issue while grouping array i have an array of object like this
var result= [
{
"user_id": 3,
"type_id": 1,
"total": 24,
"correct": 17,
"wrong": 7,
"not_attempt": 0,
"standard": "9",
"gender": "Male",
"name": "Mohammed"
},
{
"user_id": 4,
"type_id": 1,
"total": 24,
"correct": 4,
"wrong": 20,
"not_attempt": 0,
"standard": "7",
"gender": "Male",
"name": "Alex"
},
{
"user_id": 3,
"type_id": 3,
"total": 20,
"correct": 9,
"wrong": 11,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 3,
"total": 20,
"correct": 4,
"wrong": 16,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 4,
"total": 20,
"correct": 2,
"wrong": 18,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 4,
"total": 20,
"correct": 5,
"wrong": 15,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 5,
"total": 72,
"correct": 39,
"wrong": 33,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 5,
"total": 72,
"correct": 37,
"wrong": 35,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 6,
"total": 25,
"correct": 0,
"wrong": 25,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 6,
"total": 25,
"correct": 0,
"wrong": 25,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 7,
"total": 72,
"correct": 39,
"wrong": 33,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 7,
"total": 72,
"correct": 40,
"wrong": 32,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
},
{
"user_id": 3,
"type_id": 8,
"total": 12,
"correct": 6,
"wrong": 6,
"not_attempt": 0,
"name": "Mohammed",
"standard": "9",
"gender": "Male"
},
{
"user_id": 4,
"type_id": 8,
"total": 12,
"correct": 2,
"wrong": 10,
"not_attempt": 0,
"name": "Alex",
"standard": "7",
"gender": "Male"
}
]
I want to group by this object by user_id and type_id with their score i want a result like below
[
{
"user_id": 3,
"gender": "Male",
"name": "Mohammed",
"standard": "9",
"va":17,
"na":9,
"ca":2,
"sa":39,
"ma":0,
"cl":39,
"ra":6
},
{
"user_id": 4,
"gender": "Male",
"name": "Alex",
"standard": "7",
"va":4,
"na":4,
"ca":5,
"sa":37,
"ma":0,
"cl":40,
"ra":2
}
]
if user_id = 3 & type_id = 1 then i want new key to added like va and the correct score of that type_if will be added to that new key same method would apply for all type_id for their user_id, i tried below code but i am not getting result i want
var newresult = result.reduce(function(results, org) {
(results[org.user_id] = results[org.user_id] || []).push(org);
return results;
}, {})