Open In App

Underscore.js _.countBy Function

Last Updated : 16 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js _.countBy() function is used to sort a list into groups and returns a count for the number of objects in each group. It works by matching the value of each element to the other. If they match then the count of one collection increases by 1 otherwise the count of another collection/group which has that value increases by 1. It can also pass a function based on who will collect the elements and increase the count of each group. It can match both based on numbers and also by string. 

Syntax:

_.countBy(list, iterate, [context]);

Parameters:

  • List: This parameter is used to hold the list of items.
  • Iterate: This parameter is used to hold the test condition.
  • Context: The text content that needs to be displayed.

Return values:

It returns the collections as different arrays. 

Passing Math.ceil() function to the _.countBy() function:

The _.countBy() function takes the elements from the list one by one and passes it to the other function mentioned here. Here the function is taking the ceil of each number and returning its values. So, all the values of the array are counted one by one after their ceil has been taken and then counted according to whether they are the same or different.

Example: This example shows the use of the _.countBy() function by passing the Math.ceil() function.

html




<html>
 
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.countBy([2.7, 3.4, 6.6, 1.2, 2.0, 2.4],
            function (num) { return Math.ceil(num); }));
    </script>
</body>
 
</html>


Output: 

Using length() in the _.countBy() function:

Passing the array elements to the countBy() function. Then, find out the length of each element and make collections of the lengths that are the same. Finally, display the count of each collection with the respective lengths along the left. 

Example: This example shows the use of the _.countBy() function by passing the length() function.

html




<html>
 
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.countBy(['HTML', 'CSS3', 'JS', 'PHP'], 'length'));
    </script>
</body>
 
</html>


Output: 

Using one property of the array passed in the _.countBy() function:

First, declare the array (here array is ‘arr’). Choose one condition on which need to count like here ‘prop3’. Then the elements which have the same value in the ‘prop3’ will be grouped in 1 collection. The final result will contain the prop3 in the left side along with their count on the right. Like here in prop3, “Geeks” is coming two times, so its count will be 2. Console.log the final answer. 

Example: This example shows the use of the _.countBy() function.

html




<html>
 
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let arr = [
            { prop1: "10", prop2: "07", prop3: "Geeks" },
            { prop1: "12", prop2: "86", prop3: "for" },
            { prop1: "11", prop2: "58", prop3: "Geeks" }
        ];
        console.log(_.countBy(arr, 'prop3'));
    </script>
</body>
 
</html>


Output: 

Passing ‘date’ as property of the array to the _.countBy() function together:

First define an array with one property as ‘date’ of the format ‘dd-mm-yy’. Then pass the array and the ‘date’ property to the _.countBy() function. The elements having the same date will be grouped as one collection and then the count of each group will be displayed in the result. 

Example: This example shows the use of the _.countBy() function by passing “date” as property of the array.

html




<html>
 
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let orders = [
            { date: "30-60-90 Day", Name: "Kim", amount: 415 },
            { date: "30-60-90 Day", Name: "Kelly", amount: 175 },
            { date: "30 Day", Name: "Shelly", amount: 400 },
            { date: "30 Day", Name: "Sarvesh", amount: 180 }
        ];
        console.log(_.countBy(orders, "date"));
    </script>
</body>
 
</html>


Output: 

Note: These commands will not work in Google Console or in Firefox as these additional files need to be added which they didn’t have added. So, add the given links to your HTML file and then run them. 

<script type="text/javascript" src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads