How to use Lodash _.size() Method In Javascript

Lodash _.size() method is used to get the size of the given object or array.

Example: This example shows the use of the above-explained approach.

Javascript
// 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:

3

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

Similar Reads

Method 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....

Method 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....

Method 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....

Method 4: Using Lodash _.size() Method

Lodash _.size() method is used to get the size of the given object or array....

Method 5: Using the for…of loop with Object.values() Method

The for…of loop combined with the Object.values() method provides a concise way to iterate over the property values of an object. By iterating over the values and counting them, we can determine the length of the object....

Contact Us