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;
}, {})
I don't know how to calculate the scores (va, na, ca, sa, ...)
But you can do it by this way
function initializeUserObject(row) {
const { user_id, gender, name, standard } = row;
return { user_id, gender, name, standard };
}
function calculateScore(row) {
const userScoreObject = {};
// TODO : calculate score
return userScoreObject;
}
const userMap = {}
result.forEach(row => {
if (!(row.user_id in userMap)) {
userMap[row.user_id] = initiailzeUserObject(row);
}
Object.assign(userMap[row.user_id], calculateScore(row));
})
const newResult = Object.values(userMap);
or with reduce
const newResult = Object.values(result.reduce((userMap, cur) => {
userMap[cur.user_id] = Object.assign(userMap[cur.user_id] || {}, cur)
}, {}));
// you should calculate score after merge
In general you can the same code as below
var newresult = result.reduce(function(results, org) {
(results[org.user_id] = results[org.user_id] || []).push(org);
return results;
}, {})
For listing with just type_id 1 try below
var type_id = 1;
var newresult = result.reduce(function(results, org) {
if(org.type_id == type_id && org.type_id != 'undefined' )
(results[org.user_id] = results[org.user_id] || []).push(org);
return results;
}, {});
Note this will get the object keys which present in the result variable object and then later u can loop and calculate the score for newresult.
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.