How to use Object.entries() Method In Javascript
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)
Example: This example shows the use of the above-explained approach.
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
Find the length of a JavaScript object
Finding the length of a JavaScript object involves determining the number of properties or keys it contains. This is essential for assessing the size or complexity of the object, aiding in data analysis, and facilitating efficient object manipulation in JavaScript applications.
Below are the following approaches:
Table of Content
- Method 1: Using the Object.keys() method
- Method 2: Using for-in loop and hasOwnProperty() Method
- Method 3: Using Object.entries() Method
- Method 4: Using Lodash _.size() Method
- Method 5: Using the for…of loop with Object.values() Method
Contact Us