| by Arround The Web | No comments

Find the length of a JavaScript object

The popularity of JavaScript among different scripting languages is because of the objects. Due to the unavailability of the length feature in objects, it is hard to determine the length of a JavaScript object. To counter that, JavaScript is equipped with a large set of methods that can be used to compute the length. Alternatively, the strings and arrays have the length feature to compute the length of JavaScript objects.

In this article, we have demonstrated various possible methods to find the length of the objects in JavaScript. For finding the length of JavaScript object you can:

Method 1: Use the Object.keys() method for finding the length of a JavaScript object

The most common method used for finding the length of a JavaScript object is based on the Object.keys() method. The size of a JavaScript object is determined by the length feature of the Object.keys() method in JavaScript. The following example code is exercised to make use of the Object.keys() method to find the length.

Code:

// Find the length of object using keys method
let subjectResult = {
    english: 45,
    mathematics: 60,
    computer: 80,
    physics: 67,
    chemistry: 97
    statistics: 55
};

let objLength = Object.keys(subjectResult).length;
console.log(objLength);

In the above code, marks for six different subjects are stored in the subjectResult object. To find the length of the subjectResult object, the length feature of an Object.keys() method is used.

Output:

The output in the console returns the length of objLength, which is 6. It represents the total number of subjects that are stored in objLength using the length property of the Object.keys() method.

Method 2: Use the Object.values() method for finding the length of a JavaScript object

In JavaScript, another method that is used to determine the length of an object is the Object.values() method. It returns the values of the objects that are stored in it. Users can use the length property to calculate the length of a specific object in JavaScript.

An example that exercises Object.values() method is provided below:

Code:

// Find the length of object using values method
let subjectResult = {
    english: 45,
    mathematics: 60,
    computer: 80,
    physics: 67,
    chemistry: 97,
    statistics: 55
};

let objectLength = Object.values(subjectResult).length;
console.log(objectLength);

In the above JavaScript code, subjectResult is passed as an argument to the Object.values() method that returns the value of an JavaScript object.

Output:

The output shows the length of an object utilizing the method of Object.values().

Method 3: Use the Object.entries() method for finding the length of a JavaScript object

In JavaScript, one method is named Object.entries() to calculate the length of a JavaScript object. It gives the key-value pair of an object. The length is utilized to return the number of elements. The code is given below:

Code:

// Find the length of object using entries method
let subjectResult = {
    english: 45,
    mathematics: 60,
    computer: 80,
    physics: 67,
    chemistry: 97,
    statistics: 55
};

let objectLength = Object.entries(subjectResult).length;
console.log(objectLength);

The key-value pairs are passed as an argument to the Object.entries() method named as subjectResult. After that, it returns the number of entities that are stored in it.

Output:

The output represents the number of key-value pairs stored in the objectLength variable.

Method 4: Use For Loop for finding the length of a JavaScript object

For loop basically iterates over the number of elements defined in the looping condition. Here, the for loop is iterated on an object’s keys and values to get the length of an object. Let’s practice this via the following example.

Code:

// Find the length of object using for loop
let subjectResult = {
    english: 45,
    mathematics: 60,
    computer: 80,
    physics: 67,
    chemistry: 97,
    statistics: 55
};

let objLength = 0;

for (let key in subjectResult) {
    objLength++;
}

console.log(objLength);

In the above code, the objLength variable is initialized with zero. After that, start a for loop which is executed till the number of elements that are stored in subjectResult. At each iteration, the objLength variable is incremented by “1”. In the end, it is displayed as output using the console.log() method.

Output:

The output represents the number of iterations that are executed in a for-loop, which is 6.

Congratulations! In this post, you have learned to determine the length of an object in JavaScript with the help of four different methods.

Conclusion

The three static methods of JavaScript are named Object.keys(), Object.values(), and Object.entries() to find the length of an object. Moreover, you can make use of the For loop to get the length of the object. This post describes methods to determine the length of a JavaScript object. The three static methods and a For loop are explained with the help of an example to find the length of a JavaScript object.

Share Button

Source: linuxhint.com

Leave a Reply