Syntax, Parameter and Return Value
Syntax:
_.partition(collection, [predicate=_.identity])
Parameters:
-
collection (Array|Object): The collection to iterate over.
- [predicate=_.identity] (Function): The function invoked per iteration.
Return Value:
(Array) - Returns an array of two arrays: the first containing elements that satisfy the predicate and the second containing elements that do not.
Examples
Partitioning Numbers by Even and Odd:
JavaScript
var _ = require('lodash');
var numbers = [1, 2, 3, 4, 5, 6];
var [evens, odds] = _.partition(numbers, n => n % 2 === 0);
console.log(evens);
console.log(odds);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[2, 4, 6]
[1, 3, 5]
Demonstrates dividing numbers into even and odd.
Categorizing Objects Based on a Property:
JavaScript
var users = [{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': true },
{ 'user': 'pebbles', 'active': false }];
var [activeUsers, inactiveUsers] = _.partition(users, 'active');
console.log(activeUsers);
console.log(inactiveUsers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'user': 'fred', 'active': true }]
[{ 'user': 'barney', 'active': false }, { 'user': 'pebbles', 'active': false }]
Shows partitioning objects based on a boolean property.
Using a Complex Predicate Function:
JavaScript
var items = [1.2, 2.3, 3.4, 4.5];
var [greaterThanThree, others] = _.partition(items, n => n > 3);
console.log(greaterThanThree);
console.log(others);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[3.4, 4.5]
[1.2, 2.3]
An example of partitioning based on a numeric condition.
Partitioning Strings in an Array:
JavaScript
var strings = ['hello', 'world', 'lodash', 'javascript'];
var [lengthFive, otherLengths] = _.partition(strings, s => s.length === 5);
console.log(lengthFive);
console.log(otherLengths);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['hello', 'world']
['lodash', 'javascript']
Demonstrates partitioning strings based on their length.
Frequently Asked Questions
How does _.partition() handle complex data structures?
_.partition() can handle complex structures, but the predicate function needs to be designed to appropriately evaluate the condition on the elements of these structures.
Can _.partition() be used with non-array collections?
Yes, it can be used with objects, but the most common and practical use case is with arrays.
Is _.partition() efficient for large datasets?
_.partition() is generally efficient, but its performance depends on the complexity of the predicate function and the size of the dataset.
Conclusion
Lodash's _.partition() method is an efficient and effective tool for dividing a collection into two groups based on a predicate function. It streamlines the process of data categorization and segregation, making it simpler to handle complex data filtering and bifurcation tasks 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.