Javascript Object Length
In this article, we will take a look at different ways to get length of object in javascript. A javascript object is composed of key-value pairs.
Object length or size in javascript implies the number of keys in it.
There are following ways to find length of object.
Javascript
Object.keys()
method returns an array of all the keys of an object. A javascript array has a length
property which gives the total number of items in an array.Using
Object.keys()
along with length gives the total number of keys in an object, which is also, the length of the object. Example,
let obj = { product: 'iphone', manufacturer:'Apple'}; const objLength = Object.keys(obj).length; console.log('Length of object is: ' + objLength);
This prints
Length of object is: 2
Note that Object.keys()
returns the count of top level keys. If a key corresponds to another object, which itself contains some keys, then those keys will not be counted.
Thus for below object,
let obj = { product: 'iphone', manufacturer:'Apple', harware:{ ram:2, camera:16 } }; const objLength = Object.keys(obj).length;
Its length will be 3, number of top level keys.
Method 2: Using for-in loop
Javascript for-in loop is used to iterate over an object. Its syntax is
for(let key in object) { // code }
where key
is a user defined loop variable.
In every iteration, key
contains a key of the object.
Initialize a variable to 0, before the loop and increment it in every iteration. After the loop completes, the variable will contain the total number of keys in the object. Example,
let obj = { product: 'iphone', manufacturer:'Apple'}; let objLength = 0; for(let key in obj) { objLength++; } console.log('Length of object is: ' + objLength);
Output in browser console will be
Length of object is: 2
It is suitable to check if the key or property belongs to the object in question using Object.hasOwnProperty() method.
Otherwise, there may be a chance that it counts the properties of prototype or parent object as well.
So, above example may be modified as
let obj = { product: 'iphone', manufacturer:'Apple'}; let objLength = 0; for(let key in obj) { if(Object.hasOwnProperty(key) { objLength++; } } console.log('Length of object is: ' + objLength);
Object.getOwnPropertyNames()
method returns an array of object properties. Since it is an array, we can use length
property to find the length of array, which will be the size of object. Example,
let obj = { product: 'iphone', manufacturer:'Apple'}; let objLength = Object.getOwnPropertyNames(obj).length; console.log('Length of object is: ' + objLength);
Remember that getOwnPropertyNames()
returns an array of enumerable and non-enumerable properties of the object.
Enumerable means those properties which can be iterated and non-enumerable means those which can not be iterated.
This is different from Object.keys()
and for-in
loop which return only enumerable properties of the object.
As you can see, Methods 1 and 3 are concise as compared to Method 2. If you are already using a for-in loop to iterate over javascript object, then use it to find the length, otherwise go with Methods 1 or 3.