Countby category based on userId using undersocrejs.
Please refer the below script, its printing the value "Technology:2" & "Analytics:1".
Expected answer: "Technology:1" & "Analytics:1" because both the 'Technology' objects are the same userId ie. 1
arrayFlatten = [
{
area:"Digital",
category:"Technology",
userId:1,
weightedAverage:10
},
{
area:"Digital",
category:"Technology",
userId:1,
weightedAverage:20
},
{
area:"Digital",
category:"Analytics",
userId:2,
weightedAverage:30
}
]
var types = _.groupBy(arrayFlatten, 'category');
console.log(types);
var result = {};
_.each(types, function(val, key) {
console.log(key+" "+val.length);
});
console.log(result);
Thanks
Your example makes no sense, but I suspect your data is wrong. Here is some example code. I have added user 3 which represents the data to provide the expected answer in your question.