UNPKG

bbo

Version:

bbo is a utility library of zero dependencies for javascript.

18 lines (13 loc) 409 B
'use strict'; require('./get_tag.js'); var is_function = require('./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(is_function(fn) ? fn : val => val[fn]).reduce((acc, val) => { acc[val] = (acc[val] || 0) + 1; return acc; }, {}); }; module.exports = countBy;