How To Get The Length Of An Object In JavaScript

If you are looking for a way to get the length of an object in JavaScript, this article is for you. I will guide you through 4 methods: Object.keys() method, Object.values() method, Object.entries() method, and for…in loop for you to achieve your goal.

Get the Length of an Object in JavaScript

Using Object.keys() method

The Object.keys() method is used to help us get the keys of a given object. This method will return an array of the keys in the given Object.

For example:

let fruit = {
        Name: 'Strawberry',
        Categories: 'Berries',
        Origin: 'USA'
};

let lengthFruit = Object.keys(fruit).length;
console.log("The length of the given Object is: " + lengthFruit);

Output:

The length of the given Object is: 3

In the above example, the Object.keys() method returned an array of the keys of the fruit array. Since this is an array, we can use the length property to get the length of the array, which is also the length of the Object searched.

Using Object.values() method

The Object.values() method returns a new array with the elements being the values of the given Object’s properties. Like the Object.keys() method, after receiving the new array, we use the length property to get the length of the given Object in JavaScript.

For example:

let fruit = {
        Name: 'Strawberry',
        Categories: 'Berries',
        Origin: 'USA'
};

let lengthFruit = Object.values(fruit).length;
console.log("The length of the given Object is: " + lengthFruit);

Output:

The length of the given Object is: 3

Using Object.entries() method

The Object.entries() method in JavaScript is a method that returns an array of enumerable key-value pairs of a specific object. Like the two methods above, we can get the length of a given object using the length property on the new array returned by the Object.entries() method.

For example:

let fruit = {
        Name: 'Strawberry',
        Categories: 'Berries',
        Origin: 'USA'
};

let lengthFruit = Object.entries(fruit).length;
console.log("The length of the given Object is: " + lengthFruit);

Output:

The length of the given Object is: 3

Using for…in loop

In JavaScript, the for…in loop is a fundamental control statement that allows you to iterate over the properties of an object. The for...in loop iterates through each property of an object and executes the statement block in the body of the loop. 

To get the length of a given property by a for...in loop, after each iteration through the Object’s properties, we increment the counter until the loop ends.

For example:

let fruit = {
        Name: 'Strawberry',
        Categories: 'Berries',
        Origin: 'USA'
};

let count = 0;

for (key in fruit){
     count++;
};

console.log("The length of the given Object is: " + count);

Output:

The length of the given Object is: 3

Summary

Through this article, I showed you four ways to get the length of an object in JavaScript. The first three methods have the same approach, but all four methods give the expected result, and you can choose the most accessible and appropriate for your project. I hope this article will help you.

Leave a Reply

Your email address will not be published. Required fields are marked *