Socket
Book a DemoInstallSign in
Socket

count-array-values

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

count-array-values

Count the instances of each value in an array

latest
Source
npmnpm
Version
1.2.1
Version published
Maintainers
1
Created
Source

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

Keywords

array

FAQs

Package last updated on 26 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts