Share your code. Components are building blocks, you are the architect.

utils/count-by
v1.0.0
1.0.0
MIT
PUBLIC
0
0     DEPENDENCIES
Build: none
BuildNone
Counts the elements of a list according to how many match each value of a

Please configure Bit as a scoped registry to install components with NPM.Learn more

npm config set '@bit:registry' https://node.bitsrc.io
Use utils/count-by in your project
npm i @bit/bit.ramda.utils.count-by
Copied

countBy (fn:Function, list:Array) : Object

Counts the elements of a list according to how many match each value of a key generated by the supplied function. Returns an object mapping the keys produced by fn to the number of occurrences in the list. Note that all keys are coerced to strings because of how JavaScript objects work.

Acts as a transducer if a transformer is given in list position.

Example

var numbers = [1.0, 1.1, 1.2, 2.0, 3.0, 2.2];
     R.countBy(Math.floor)(numbers);    //=> {'1': 3, '2': 2, '3': 1}

     var letters = ['a', 'b', 'A', 'a', 'B', 'c'];
     R.countBy(R.toLower)(letters);   //=> {'a': 3, 'b': 2, 'c': 1}

Arguments

fn: Function

The function used to map values to keys.

list: Array

The list to count elements from.

Returns

Object

An object mapping keys to number of occurrences in the list.