Friday, May 8, 2026
HomeLanguagesJavascriptLodash _.frequencies() Method

Lodash _.frequencies() Method

The Lodash _.frequencies() method takes an array and returns a mapping object whose keys are the values of the array’s elements and values are counts of that key appeared in that array.

Syntax:

_.frequencies( array );

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

  • array: The given array from which mapping is to be created.

Return Value: This method returns a created mapping object.

Note: This will not work in normal JavaScript because it requires the lodash.js contrib library to be installed. Lodash.js contrib library can be installed using the following command:

npm install lodash-contrib

Example 1:




// Defining underscore lodash variable 
var _ = require('lodash-contrib'); 
  
// Array 
var array = ["Geeks", "Geeks", "GFG",  
             "Computer_Science_Portal",  
             "Geeks", "GFG"]; 
    
var obj = _.frequencies(array); 
  
// Printing object
console.log("Original Array : ", array); 
console.log("Frequency of elements : ", obj);


Output:

Original Array : [“Geeks”, “Geeks”, “GFG”, “Computer_Science_Portal”, “Geeks”, “GFG”]
Frequency of elements : Object {Computer_Science_Portal: 1, GFG: 2, Geeks: 3}

Example 2:




// Defining underscore lodash variable 
var _ = require('lodash-contrib'); 
  
// Array 
var array = []; 
    
var obj = _.frequencies(array); 
  
// Printing object
console.log("Original Array : ", array); 
console.log("Frequency of elements : ", obj);


Output:

Original Array : []
Frequency of elements : Object {}

Example 3:




// Defining underscore lodash variable 
var _ = require('lodash-contrib'); 
  
// Array 
var array = [1, 1, 1, 1, 1, 1, 3,
          3, 3, 4, 4, 4, 5, 5, 5, 
          6, 6, 6, 6, 7, 7, 8, 10]; 
    
var obj = _.frequencies(array); 
  
// Printing array
console.log("Original Array : ", array); 
console.log("Frequency of elements : ", obj);


Output:

Original Array : [1, 1, 1, 1, 1, 1, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 8, 10]
Frequency of elements : Object {1: 6, 3: 3, 4: 3, 5: 3, 6: 4, 7: 2, 8: 1, 10: 1}

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6962 POSTS0 COMMENTS