My data needs to be aggregated, and while MongoDB aggregation would make this easy, I prefer to do it on the client side. Utilizing LoDash or Underscore should help with this task.
Let's consider the sample data below:
var data = [
{
"uId" : "u1",
"qId" : "q1",
"share" : 2,
"star" : 2,
"score" : 5
},
{
"uId" : "u1",
"qId" : "q2",
"share" : 2,
"star" : 2,
"score" : 5
},
{
"uId" : "u2",
"qId" : "q1",
"share" : 2,
"star" : 2,
"score" : 5
},
{
"uId" : "u2",
"qId" : "q2",
"share" : 3,
"star" : 3,
"score" : 7
},
{
"uId" : "u3",
"qId" : "q1",
"share" : 3,
"star" : 3,
"score" : 7
},
{
"uId" : "u3",
"qId" : "q2",
"share" : 3,
"star" : 3,
"score" : 7
}
]
The desired result is as follows:
result = [
{
"uId" : "u3",
"qId" : 2,
"share" : 6,
"star" : 6,
"score" : 14
},
{
"uId" : "u2",
"qId" : 2,
"share" : 5,
"star" : 5,
"score" : 12
}
]
The output should be sorted by highest score and limited to showing only 2 results.
Thank you for your assistance.