I am trying to calculate the unique count of users.hobbies using Lodash. The data looks like this:
var users = [
{ 'user': 'barney', 'age': 36, 'hobbies': ['soccer','basketball'] },
{ 'user': 'fred', 'age': 40, 'hobbies': ['soccer', 'golf'] },
{ 'user': 'tom', 'age': 25, 'hobbies': ['golf'] }
];
I have tried:
_.countBy(users, 'hobbies');
// { 'soccer,basketball': 1, 'soccer,golf': 1, golf: 1 }
The desired output is:
// { 'soccer':2, 'basketball':1, 'golf':2}
I guess I would need to split the value of users.hobbies but I am not exactly sure how to apply it.
_.countBy(_.flatten(users.map(u => u.hobbies)))