How to Count the Number of Properties of the JavaScript Object

Dhananjay Kumar / Monday, April 09, 2018

While working with JavaScript, I come across a requirement to count a number of properties in a JavaScript object.  I found two ways to find the number of properties in an object. They are as follows:

  1. Using for loop
  2. Using Object.keys

Consider an object, “cat”, as demonstrated below:

var cat = {
 
    name: 'foo',
    age: 9
}

You can find a number of properties by iterating in a for loop and update counter, as shown in the below listing:

let count = 0;
for (var c in cat) {
 
    count = count + 1;
}
console.log(count);// 2

Above code will print “2” as output.  

The above approach not only prints the object’s own enumerable properties, but it also prints properties of objects to chich it is linked.  To further understand it, let us consider listing:

var animal = {
 
    canRun: true
}
 
var cat = {
 
    name: 'foo',
    age: 9
}
 
cat.__proto__ = animal;

There are two objects, cat and animal, and the cat object is linked to an animal object using __proto__ property.  Now, when you use for loop to iterate and count a number of properties, it will also count enumerable properties of the animal object. Therefore, the code listing below will print “3”.

var animal = {
 
    canRun: true
}
 
var cat = {
 
    name: 'foo',
    age: 9
}
 
cat.__proto__ = animal;
 
let count = 0;
for (var c in cat) {
 
    count = count + 1;
}
console.log(count);// 3

JavaScript for loop will iterate all linked properties of the object.

To count the object’s own enumerable properties, you may consider using another approach, Object.keys(), which only enumerates the object’s own enumerable properties. It does not enumerate the object’s linked properties.

Moving forward, let us again consider cat object which is linked to animal object and count number of properties using Object.keys:

var animal = {
 
    canRun: true
}
 
var cat = {
 
    name: 'foo',
    age: 9
}
 
cat.__proto__ = animal;
 
var count = Object.keys(cat).length;
console.log(count);

Now you will get “2” printed as output.

Object.keys only enumerates the object’s own enumerable properties.

If the object’s property enumerable is set to false, then it is not a member of Object.keys array.  Let us again consider cat object and set its name property enumerable to false.

var cat = {
 
    name: 'foo',
    age: 9
}
 
Object.defineProperty(cat, 'name', { enumerable: false });

 

Now, when you use Object.keys to find a number of properties, it will count only one.

var count = Object.keys(cat).length;
console.log(count);  // print 1

In closing, these are the two ways that you can use to find a number of properties in a JavaScript object.  

If you like this post, please share it. In addition, if you haven’t checked out Infragistics Ignite UI for Angular components, be sure to do so! We’ve got 30+ material based Angular components to help you code speedy web apps faster.