Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Ramda provides the countBy function for tallying repeated entries within an array.

countBy takes a function that determines the property to be counted and the array to be counted as arguments.

Here's an example of how to use countBy:

const R = require('ramda')

const animals = ['dog', 'cat', 'dog', 'mouse', 'cat', 'dog']

const countAnimals = R.countBy(R.identity)

console.log(countAnimals(animals))  // { 'dog': 3, 'cat': 2, 'mouse': 1 }

In this example, countBy(R.identity) counts the number of occurrences of each animal in the animals array.

The output is an object with the animal names as keys and the number of occurrences as values.