0

I have a collection in which I have to use aggregation using javascript only. I have tried couple of things using Lodash library but with no luck. If you could guide me with someway of grouping a collection,' I think that should resolve.

var sample = [
        {
           

            "DESCRIPTOR": "HAPPY",
            "DESCRIPTOR_ID": 400001,
            "QUESTION_ID": "A_QUES_1",
            "CHOICE": "A",
            "SCORE": 1,
            "__v": 0
        },
        {
           

            "DESCRIPTOR": "HAPPY",
            "DESCRIPTOR_ID": 400001,
            "QUESTION_ID": "A_QUES_2",
            "CHOICE": "B",
            "SCORE": 2,
            "__v": 0
        },
{
           

            "DESCRIPTOR": "SAD",
            "DESCRIPTOR_ID": 400002,
            "QUESTION_ID": "B_QUES_1",
            "CHOICE": "A",
            "SCORE": 2,
            "__v": 0
        },
        {
           

            "DESCRIPTOR": "SAD",
            "DESCRIPTOR_ID": 400002,
            "QUESTION_ID": "B_QUES_2",
            "CHOICE": "B",
            "SCORE": 2,
            "__v": 0
        }
    ]

I expect something like below,

 result = [{"DESCRIPTOR": "HAPPY", "TOTAL_SCORE":3}, {"DESCRIPTOR": "SAD", "TOTAL_SCORE":4}]

Can you help me writing this code in Javascript only? Thanks

Shah Rukh K
  • 559
  • 1
  • 5
  • 19
  • I have found an answer already. Duplicate to [This Question](https://stackoverflow.com/questions/29364262/how-to-group-by-and-sum-array-of-object) – Shah Rukh K Nov 12 '20 at 06:39
  • Does this answer your question? [how to group by and sum array of object?](https://stackoverflow.com/questions/29364262/how-to-group-by-and-sum-array-of-object) – Brian Lee Nov 12 '20 at 06:46

2 Answers2

0

You can use array reduce method for that,

let result = sample.reduce((prev, curr) => {
   const descriptorName = curr.DESCRIPTOR;
   const index = prev.findIndex((item) => item.DESCRIPTOR === descriptorName);
   if(index > -1) {
      prev[index].SCORE += curr.SCORE;
   } else {
      prev.push({'DESCRIPTOR': descriptorName, 'SCORE': curr.SCORE})
   }
   return prev;
}, []);

console.log(result);
Md Sabbir Alam
  • 4,937
  • 3
  • 15
  • 30
0

The code below grouped the objects based on DESCRIPTOR_ID. First traverse the arrays using Array.prototype.reduce() method and get the descriptor_id and make it as key and count the total score for each key. Finally get the values using Object.values() method.

const sample = [
  {
    DESCRIPTOR: 'HAPPY',
    DESCRIPTOR_ID: 400001,
    QUESTION_ID: 'A_QUES_1',
    CHOICE: 'A',
    SCORE: 1,
    __v: 0,
  },
  {
    DESCRIPTOR: 'HAPPY',
    DESCRIPTOR_ID: 400001,
    QUESTION_ID: 'A_QUES_2',
    CHOICE: 'B',
    SCORE: 2,
    __v: 0,
  },
  {
    DESCRIPTOR: 'SAD',
    DESCRIPTOR_ID: 400002,
    QUESTION_ID: 'B_QUES_1',
    CHOICE: 'A',
    SCORE: 2,
    __v: 0,
  },
  {
    DESCRIPTOR: 'SAD',
    DESCRIPTOR_ID: 400002,
    QUESTION_ID: 'B_QUES_2',
    CHOICE: 'B',
    SCORE: 2,
    __v: 0,
  },
];

const ret = Object.values(
  sample.reduce((prev, c) => {
    const p = prev;
    const key = c.DESCRIPTOR_ID;
    if (!p[key]) p[key] = { DESCRIPTOR: c.DESCRIPTOR, TOTAL_SCORE: c.SCORE };
    else p[key].TOTAL_SCORE += c.SCORE;
    return p;
  }, {})
);
console.log(ret);
mr hr
  • 3,162
  • 2
  • 9
  • 19