count-array-values

Count the instances of each value in an array


Keywords
array, count, score, frequency, total, tally
License
MIT
Install
npm install count-array-values@1.2.1

Documentation

count-array-values Build Status

Count the instances of each value in an array, ignoring any non-string values.

Installation

npm install count-array-values --save

Usage

var count = require('count-array-values')
count(['foo', 'bar', 'Bar', 451, 'bar', 'bar', 'baz', 'foo', null, undefined])

// [
//   {value: 'bar', count: 3},
//   {value: 'foo', count: 2},
//   {value: 'Bar', count: 1},
//   {value: 'baz', count: 1},
// ]

If you'd like to use a more relevant property name than value, specify it as the second argument when calling the function:

count(['apple', 'banana', 'apple'], 'fruit')

// [
//   {fruit: 'apple', count: 2},
//   {fruit: 'banana', count: 1}
// ]

You can also override the name of the count property:

const packages = ['express', 'lodash', 'express', 'lodash', 'express', 'banana']
count(packages, 'package', 'dependents')

// [
//   {package: 'express', dependents: 3},
//   {package: 'lodash', dependents: 2},
//   {package: 'banana', dependents: 1}
// ]

Tests

npm install
npm test

Dependencies

None

Dev Dependencies

  • standard: JavaScript Standard Style
  • standard-markdown: Test your Markdown files for Standard JavaScript Style™
  • tap-spec: Formatted TAP output like Mocha's spec reporter
  • tape: tap-producing test harness for node and browsers

License

MIT