Syntax, Parameter and Return Value
Syntax:
_.countBy(collection, [iteratee=_.identity])
Parameters:
-
collection (Array|Object): The collection to iterate over.
- [iteratee=_.identity] (Function): The iteratee to transform keys.
Return Value:
(Object) - Returns the composed aggregate object.
Examples
Counting Numbers by Even and Odd:
JavaScript
var _ = require('lodash');
var numbers = [1, 2, 3, 4, 5, 6];
var countByEvenOdd = _.countBy(numbers, num => num % 2 === 0 ? 'even' : 'odd');
console.log(countByEvenOdd);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'odd': 3, 'even': 3 }
This example demonstrates counting numbers based on whether they are even or odd.
Categorizing Strings by Length:
JavaScript
var words = ['sky', 'wood', 'forest', 'cliff', 'castle', 'river'];
var countByLength = _.countBy(words, 'length');
console.log(countByLength);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ '3': 2, '5': 2, '6': 2 }
Here, strings are categorized and counted based on their length.
Counting Objects by Property:
JavaScript
var objects = [{ 'type': 'fruit', 'name': 'apple' },
{ 'type': 'vegetable', 'name': 'carrot' },
{ 'type': 'fruit', 'name': 'banana' }];
var countByType = _.countBy(objects, 'type');
console.log(countByType);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'fruit': 2, 'vegetable': 1 }
An example showing how to count objects based on a shared property.
Counting with Custom Function:
JavaScript
var items = [1.3, 2.1, 2.4];
var countByIntegerPart = _.countBy(items, Math.floor);
console.log(countByIntegerPart);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ '1': 1, '2': 2 }
Demonstrates using a custom function to determine the counting criterion.
Frequently Asked Questions
Can _.countBy() handle mixed data types in a collection?
Yes, it can categorize and count mixed data types, provided the iteratee function can handle them appropriately.
How does _.countBy() differ from _.groupBy()?
While _.groupBy() groups elements without counting, _.countBy() categorizes elements and provides a count for each category.
Is _.countBy() efficient for large collections?
_.countBy() is generally efficient for large collections, but its performance may vary depending on the complexity of the iteratee function.
Conclusion
Lodash's _.countBy() method is a powerful and flexible tool for categorizing and counting elements in a collection based on specific criteria. It aids in data analysis and manipulation by providing insights into the distribution and frequency of data elements. This method exemplifies the utility of Lodash in simplifying and enhancing data handling in JavaScript.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.