Skip to content

Instantly share code, notes, and snippets.

@alex-cory
Created November 22, 2017 18:47
Show Gist options
  • Save alex-cory/a78249ee5f5f195fa25b64c6198b6ffd to your computer and use it in GitHub Desktop.
Save alex-cory/a78249ee5f5f195fa25b64c6198b6ffd to your computer and use it in GitHub Desktop.
Counts the number of X in an array
/**
* @example
* const list = ['a', 'b', 'c', 'c', 'c']
* const numOfCs = count(list, letter => letter === 'c')
* console.log(numOfCs) // 3
*/
const count = (ls, cb) => ls.reduce((count, item) => cb(item) ? ++count : count, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment