In this article, we will explore the method for determining the length of an object in JavaScript. Prior to that, it is essential to familiarize ourselves with the concept of a JavaScript object.
What is the JavaScript object?
An object serves as a vessel that encapsulates both attributes and functions. In the context of everyday life, examples of objects include items like backpacks, creatures, and more.
To illustrate with a real-world example, consider the following ac object:
Object properties
An ac object possesses various attributes, including name, type, color, and price. The attributes are as follows:
ac.name = blue star,
ac.type = split ac,
ac.color = white,
ac.price = 45000
Object methods
A practical air conditioner (AC) includes several functions, including start, lowtemp, swing, and stop. Below is a description of these methods:
ac.start,
ac.lowtemp,
ac.swing,
ac.stop
We can get the length of a JavaScript object utilizing the following methods:
- Utilizing Object.entries Method
- Utilizing the Object.keys Method
- Utilizing Object.objsize Method
- Utilizing Object.values Method
- Utilizing for-in loop
- Utilizing the Object.getOwnPropertyNames Method
- Utilizing the Object.getOwnPropertyNames Method
- Utilizing for-in loop with hasOwnProperty Method
- Utilizing the for…of loop with Object.values Method
- Utilizing Lodash _.size Method
We will understand each approach through the use of examples.
Utilizing Object.entries Method
This approach is employed to yield an array of key-value pairs that exist within the JavaScript object. The key-value pairs are supplied as arguments to the object, after which the Object.entries method will determine the total count of properties included in the object.
Syntax:
Object.entries(obj)
Code:
function getObjectLength() {
personObject = {
id: 105,
name: 'Kapil',
age: 36,
department: 'Software Engineering'
}
const objectLength = Object.entries(personObject).length;
console.log(objectLength);
}
getObjectLength();
Output:
Utilizing the Object.keys method
This approach is employed to retrieve the count of properties present within the object. Subsequently, the Object.keys method is used to determine the length of the JavaScript object.
Syntax:
Object.keys(obj)
Code:
function getObjectLength() {
personObject = {
id: 105,
name: 'Kapil',
age: 36,
department: 'Software Engineering'
}
const objectLength = Object.keys(personObject).length;
console.log(objectLength);
}
getObjectLength();
Output:
Utilizing Object.objsize Method
The Object.objsize function is employed to determine the size of an object. To obtain the length of the object, we will pass the object as an argument to the Object.objsize function and then access its length property.
Syntax:
Object.objsize(obj)
Code:
Object.objsize = function(obj) {
let size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key))
size++;
}
return size;
};
let objectBooks = {
'book1': 'Harry Potter',
'book2': 'Pride and Prejudice',
'book3': 'The Lord of the Rings',
'book4': 'Crime and Punishment',
'book5': 'Great Expectations'
};
let count = Object.objsize(objectBooks);
console.log(count);
Output:
Utilizing Object.values Method
This approach is employed to determine the length of the object. We will provide the object as an argument to the Object.values method and then access the length property to retrieve the length.
Syntax:
Object.values(obj)
Code:
let objectBooks = {
'book1': 'Harry Potter',
'book2': 'Pride and Prejudice',
'book3': 'The Lord of the Rings',
'book4': 'Crime and Punishment',
'book5': 'Great Expectations'
};
let length = Object.values(objectBooks).length
console.log(length);
Output:
Utilizing for-in loop
The for-in loop is employed to traverse the properties of an object, allowing you to determine the number of properties contained within a JavaScript object.
Syntax:
for (key in object) {
// code block
}
Code:
const objectPerson = {
name: 'Kamal',
age: 32,
job: 'teacher',
salary: '36000'
};
let length = 0;
for (const key in objectPerson) {
length++;
}
console.log(length);
Output:
Utilizing the Object.getOwnPropertyNames Method
The Object.getOwnPropertyNames method is employed to retrieve the count of properties present within an object.
Syntax:
Object.getOwnPropertyNames(obj)
Code:
const objCars = {
car1: 'Toyoto',
car2: 'Ford',
car3: 'BMW' };
const arrFromObj = Object.getOwnPropertyNames(objCars);
console.log(arrFromObj.length);
Output:
Utilizing for-in loop and hasOwnProperty Method
The hasOwnProperty function is employed to determine if a specific key exists within the object. By using the for-in loop, we can iterate through each key, which allows us to ascertain the total number of properties in the object.
Syntax:
let key, count = 0;
// utilized to check whether each key consists of its own property
for (key in exampleObj) {
if (exampleObj.hasOwnProperty(key))
// If the key is found then add it to the total length
count++;
}
objectLength = count;
Code:
function getObjectLength() {
studentObject = {
student_id: 105,
student_name: 'Bhakti',
student_age: 18,
student_stream: 'science'
}
let key, count = 0;
for (key in studentObject) {
if (studentObject.hasOwnProperty(key))
count++;
}
studentObject = count;
console.log(studentObject);
}
getObjectLength();
Output:
Utilizing the for...of loop with Object.values Method
The Object.values function is employed to retrieve an array containing the values of all properties found within an object. To determine the length of the object, the for-of loop iterates over each value contained in the object.
Syntax:
let count = 0;
for (const value of Object.values(demoObject)) {
count++;
}
Code:
const demoObject = { a1: 1, a2: 2, a3: 3, a4: 4, a5: 5 };
let count = 0;
for (const value of Object.values(demoObject)) {
count++;
}
console.log(count);
Output:
Utilizing Lodash _.size Method
This approach is employed to determine the dimensions of the specified object. Initially, the object is supplied to the _.size function, which subsequently returns the count of properties present within the JavaScript object.
Code:
const _ = require("lodash");
let cars = _.size({ 'car1': 'BMW', 'car2': 'Toyoto', 'car3': 'Ford', 'car4': 'Honda' });
console.log(cars);
Output:
Conclusion:
In this article, we have explored the ways to determine the length of a JavaScript object. Through various examples, we have examined multiple techniques to achieve this understanding.