I have an array of objects:
[
{'year': 2019, 'a_academic': 3, 'f_security': 4, ..., 'k_services': 3},
{'year': 2019, 'a_academic': 2, 'f_security': 2, ..., 'k_services': 4},
{'year': 2019, 'a_academic': 3, 'f_security': 3, ..., 'k_services': 1},
...
{'year': 2019, 'a_academic': 3, 'f_security': 3, ..., 'k_services': 3},
]
How to count multiple properties values, then grouping it, and save it in a new object, e.g.:
{
'a_academic': {
4: 0,
3: 3,
2: 1,
1: 0
},
'f_security': {
4: 1,
3: 2,
2: 1,
1: 0
},
...,
'k_services': {
4: 1,
3: 2,
2: 0,
1: 1
}
}
I'm able to do it using reduce and manually accessing the key, but only for one property:
let count = array.reduce((res, cur) => {
res[cur.a_academic] = res[cur.a_academic] ? res[cur.a_academic] + 1 : 1;
return res;
}, {});
console.log(count);
Result:
{
3: 3,
2: 1
}
How to implement this efficiently so it works for all other properties, without manually accessing it?