bbo
Version:
bbo is a utility library of zero dependencies for javascript.
16 lines (12 loc) • 385 B
JavaScript
import './get_tag.js';
import isFunction from './is_function.js';
/**
* Groups the elements of an array based on the given function and returns the count of elements in each group.
*/
var countBy = (arr, fn) => {
return arr.map(isFunction(fn) ? fn : val => val[fn]).reduce((acc, val) => {
acc[val] = (acc[val] || 0) + 1;
return acc;
}, {});
};
export default countBy;