Underscore.js _.countBy Function
Last Updated :
16 Jan, 2024
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=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js">
</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=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js">
</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=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js">
</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=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js">
</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>
Similar Reads
Underscore.js _.chunk() Function
_.chunk() function: It is used to split a passed array into a number of arrays containing the number of elements given in the second parameter of the _.chunk() function.We can convert a single array into a number of arrays using this function.Also, the number of elements which the resultant array wi
3 min read
Underscore.js _.constant() Function
Underscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. The _.constant() function is used to create a function that returns the parameter given to it. It is almost same as _.identity() function. Note: It is very necessary to link the underscore C
2 min read
Underscore.js _.contains Function
Underscore.js _.contains() function is used to check whether a particular item is given in the list or not. This function needs to pass the list to this function. If the list contains a large of items then simply define the list earlier and pass the name of the list as an argument to the _.contains(
3 min read
Underscore.js _.compact() Function
Underscore.js _.compact() function is used to return an array after removing all the false values. The false values in JavaScript are NaN, undefined, false, 0, null, or an empty string. Its output is an array containing all the even values like the elements of the array, numbers, alphabets, characte
3 min read
Underscore.js _.chain() Function
Underscore.js _.chain() function is an inbuilt function in the Underscore.js library of JavaScript which is used to find a wrapped object. Moreover, invoking the methods on this object will continue to return the wrapped objects until the value is invoked. Syntax:_.chain(obj);Parameters:obj: It is t
1 min read
Underscore.js _.create() Function
Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.create() function is an inbuilt function in Underscore.js library of JavaScript which is used to cr
2 min read
Underscore.js _.each() Function
Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.each() function is an inbuilt function in Underscore.js library of JavaScript which is used to retu
3 min read
Underscore.js _.find() Function
Underscore.js _.find() function looks at each element of the list and returns the first occurrence of the element that satisfies the condition. If any element of the list does not satisfy the condition then it returns the undefined value. Syntax:_.find(list, predicate, [context])Parameters:list: Thi
1 min read
Underscore.js _.functions() Function
Underscore.js _.functions() function is used to return the sorted list of all methods that are present in an object. Syntax:_.functions(object);Parameters: object: It contains the object element that holds the elements [key, value] pair.Return Value: It returns the sorted list of all methods that ar
1 min read
Underscore.js _.after() Function
Underscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.after() function is an inbuilt function in Underscore.js library of JavaScript which is used to cre
2 min read