I have an array of objects like this:
[
{"question":"Q1","answer":"my answer 2"},
{"question":"Q1","answer":"my answer"}
{"question":"Q1","answer":"my answer"}
{"question":"Q2","answer":"answer 2"}
]
I would like to group by the question keys and return the counts of each answer.
e.g.
{
"Q1": [{
"answer": "my answer",
"count": 2
}, {
"answer": "my answer 2",
"count": 1
}],
"Q2": [{
"answer": "answer 2",
"count": 1
}]
}
,
I am able to groupBy questions using:
.groupBy("question") and count occurances of values using .countBy() but I am not sure how to combine the grouping and counting functions to achieve the desired output?