Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptLodash _.size() Method

Lodash _.size() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc.
The _.size() method gets the size of collection by returning its length for array such as values or the number of own enumerable string keyed properties for objects.

Syntax:

_.size(collection)

Parameters: This method accepts single parameter as mentioned above and described below:

  • collection: This parameter holds the collection to inspect.

Return Value: This method returns the collection size.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array and use _.size() method
var gfg = _.size([12, 14, 36, 29, 10]);
  
// Printing the output 
console.log(gfg);


Output:

5

Example 2:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array and use _.size() method
var gfg = _.size({ 'p': 1, 'q': 2, 'r': 5});
  
// Printing the output 
console.log(gfg);


Output:

3

Example 3:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array and use _.size() method
var gfg1 = _.size('neveropen');
var gfg2 = _.size('computer science');
  
// Printing the output 
console.log(gfg1, gfg2);


Output:

13, 16

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.

RELATED ARTICLES

Most Popular

Recent Comments