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