Files
30-seconds-of-code/test/countBy/countBy.js
2018-01-17 13:40:40 -05:00

6 lines
188 B
JavaScript

const countBy = (arr, fn) =>
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => {
acc[val] = (acc[val] || 0) + 1;
return acc;
}, {});
module.exports = countBy