I have this set of array in my javascript.
var array = [
['Jack', 'A'],
['Jack', 'A'],
['Sara', 'A'],
['Jack', 'B'],
['Peter', 'B'],
['Sara', 'C'],
['Sara', 'B'],
['Sara', 'A'],
['Sara', 'B'],
['Peter', 'B'],
['Peter', 'A'],
['Peter', 'C']
];
my goal is to be arranged like this as an array.
Jack: A : 2
Jack: B : 1
Sara: A : 2
Sara: B : 2
Sara: C : 1
Peter: A : 1
Peter: B : 2
Peter: C : 1
I am trying to code it like this but I can’t get it right
let counts = {};
array.forEach(element => {
counts[element] = (counts[element] || 0) + 1;
});
does anyone has an idea about this one?
Thank you..
2
Answers
I think you want something like this:
You could group by both values and map the count.