0

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;
                  }, {})
AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
Mohammed
  • 43
  • 1
  • 7
  • 1
    where does `va`, `ca`, `sa`, etc come from? – DecPK Jun 08 '21 at 05:57
  • Look at this to merge two objects - [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – ShivCK Jun 08 '21 at 06:00
  • Btw where is your second array of objects to merge with first one? (if properties are needed to be generated dynamically , I didn't understand how to it.) – ShivCK Jun 08 '21 at 06:02
  • va,ca,and sa are new object key i want them in new array of object for type_id =1 va for type_id =2 na like so – Mohammed Jun 08 '21 at 08:38

2 Answers2

0

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
Yonghoon Lee
  • 2,217
  • 1
  • 12
  • 8
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mohammed Abrar Ahmed
  • 2,290
  • 3
  • 15
  • 33