I've got lodash to group my object by userId but I know want it group again by name values that are equal.
So for example I have the data:
"data": [
{
"name": "Red",
"userId": "batman"
},
{
"name": "Red",
"userId": "batman"
},
{
"name": "Blue",
"userId": "batman"
}
]
I'd like something that like:
[
{userId: "Batman",
name: {
"red": 2
"blue": 1
}}
]
Basically to help me give a representation from which I can produce something like:
Red - 2 - batman
Blue - 1 - batman
I have this so far
console.log(
chain(data)
.groupBy("userId")
.map((value, key) => ({ userId: key, name: value }))
.value()
);
But this only gives me group by userId.
Any ideas/help would be appreciated.
Thanks
UPDATE - solved it by also needing a second function:
function filterArr(data: any, key: any){
return data.reduce( (result: any, current: any) => {
if(!result[current[key]]){
result[current[key]] = 1;
} else {
result[current[key]] += 1;
}
return result;
}, {})
}
with:
console.log(
chain(data)
.groupBy("userId")
.map((value, key) => ({ userId: key, name: filterArr(value, "name") }))
.value()
);