npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

count-keys

v1.1.2

Published

A JS function for counting the number of keys there are in objects and arrays, including nested objects and arrays

Downloads

154

Readme

count-keys

A JS function for counting the number of keys there are in objects and arrays, including nested objects and arrays.

npm install count-keys --save

The first parameter in countKeys is the object you wish to count the number of keys in. The second parameter takes various options that allow you to control the output.

countKeys(object, [options])

Contents

Default functionality

By default, every key in the object will increase the increment of the return value by 1. If a value in the object is another object, it will also count all the keys in that child object. Since arrays are essentially objects with numbered keys (['a', 'b','c'] being similar to {0:'a', 1:'b', 2:'c'}) this will also count array items by default.

var countKeys = require('count-keys');

var object = {
  one: {
    two : 'string',
    three : {
      four: ['five', 'six']
    }
  },
  seven: 'string',
  eight: {
    nine : 'string',
  }
}

var keyCount = countKeys(object);

// keyCount = 9

Excluding arrays

Arrays and objects aren't exactly the same thing though so it's understandable that you might not want to include array items in the index count. To turn off array counting, use the arrays setting.

var countKeys = require('count-keys');

var object = {
  one: {
    two : 'string',
    three : {
      four: ['string', 'string']
    }
  },
  five: 'string',
  six: {
    seven : 'string',
  }
}

var keyCount = countKeys(object, { arrays: false })

// keyCount = 7

Excluding sub-objects

Maybe you don't want to know how deep the rabit hole goes. Maybe you just want to count the blades of grass at the entrance. For a simple top level only counting of the number of keys there are in an object, set recursive to false.

var countKeys = require('count-keys');

var object = {
  one: {
    keyA : 'string',
    keyB : {
      keyC: ['string', 'string']
    }
  },
  two: 'string',
  three: {
    keyD : 'string',
  }
}

var keyCount = countKeys(object, { recursive: false })

// keyCount = 3

Excluding keys below a certain depth

Similar to recursive:false. maxDepth will allow you to define a specific depth that you do not want to surpass when counting. maxDepth:1 is essentially the same as recursive:false.

var countKeys = require('count-keys');

var object = {
  one: {
    two : 'string',
    three : {
      keyA: ['string', 'string']
    }
  },
  four: 'string',
  five: {
    six : 'string',
  }
}

var keyCount = countKeys(object, { maxDepth: 2 })

// keyCount = 6

Custom filtering

If you aren't happy with the results you are getting back from the function, you might need to do custom filtering.

var countKeys = require('count-keys');

var object = {
  one: {
    a : 'string',
    two : {
      three: ['four', 'five']
    }
  },
  b: 'string',
  six: {
    c : 'string',
  }
}

var keyCount = countKeys(object, { filter: function(item){
    // available values in "item":
    // item.key, item.value, item.count, item.parent, item.depth

    if (item.value === 'string') return false;

    //The filter will default to "true" if it returns "undefined"
}})

// keyCount = 6

These are the values you have available to you in the "item" variable:

  • key: The key for the current key/value pair
  • value: The value for the current key/value pair
  • count: The count as it is so far
  • parent: The object containing the current key/value pair
  • depth: The depth of the current key/value pair