Finding the length of a JavaScript object refers to determining how many key-value pairs JavaScript object contains. This is often necessary when you need to know the size of the data structure for iterations, validations, or other operations involving object properties.
1. Using the Object.keys() method
The Object.keys() method is used to return the object property name as an array. The length property is used to get the number of keys present in the object. It gives the length of the object.
Syntax
objectLength = Object.keys(exampleObject).lengthfunction getObjectLength() {
// Declare an object
exampleObject = {
id: 1,
name: 'Arun',
age: 30
}
// Using Object.keys() method to get length
objectLength = Object.keys(exampleObject).length;
console.log(objectLength);
}
getObjectLength();
Output
3
2. Using for-in Loop and hasOwnProperty() Method
The hasOwnProperty() method is used to return a boolean value indicating whether the object has the specified property as its property. This method can be used to check if each key is present in the object itself. The contents of the object are looped through and if the key is present, the total count of keys is incremented. This gives the length of the object.
Syntax
let key, count = 0;
// Check if every key has its own property
for (key in exampleObject) {
if (exampleObject.hasOwnProperty(key))
// If the key is found, add it to the total length
count++;
}
objectLenght = count;
function getObjectLength() {
// Declare an object
exampleObject = {
id: 1,
name: 'Arun',
age: 30,
department: 'sales'
}
let key, count = 0;
// Check if every key has its own property
for (key in exampleObject) {
if (exampleObject.hasOwnProperty(key))
// If key is found, add it
// to total length
count++;
}
objectLength = count;
console.log(objectLength);
}
getObjectLength();
Output
4
3. Using Object.entries() Method
JavaScript Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually.
Syntax
Object.entries(obj)function getObjectLength() {
// Declare an object
exampleObject = {
id: 1,
name: 'Arun',
age: 30,
department: 'sales'
}
const objectLength = Object.entries(exampleObject).length;
console.log(objectLength);
}
getObjectLength();
Output
4
4. Using Lodash _.size() Method
To find the length of a JavaScript object using Lodash’s _.size() method, pass the object to _.size(). This function returns the number of own enumerable properties in the object, making it a simple and effective way to determine the object's length.
// Requiring the lodash library
const _ = require("lodash");
// Original array and use _.size() method
let gfg = _.size({ 'p': 1, 'q': 2, 'r': 5 });
// Printing the output
console.log(gfg);
Output
3JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.