Files
30-seconds-of-code/test/countBy/countBy.js
2018-10-15 12:18:24 +04:00

7 lines
198 B
JavaScript

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