Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Partitioning Numbers by Even and Odd:
4.2.
JavaScript
4.3.
Categorizing Objects Based on a Property:
4.4.
JavaScript
4.5.
Using a Complex Predicate Function:
4.6.
JavaScript
4.7.
Partitioning Strings in an Array:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
How does _.partition() handle complex data structures?
5.2.
Can _.partition() be used with non-array collections?
5.3.
Is _.partition() efficient for large datasets?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.partition() Method

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Separating elements of a collection based on a predicate is a common task in data processing and manipulation. Lodash addresses this need with the _.partition() method. It splits a collection into two arrays: one whose elements satisfy the predicate function and another whose elements do not. 

Lodash _.partition() Method

This article will explore the _.partition() method, illustrating its functionality, syntax, and utility through examples and FAQs.

Why This Function is Used

The _.partition() function divides a collection into two groups based on whether or not they pass a specified test. This is particularly useful in scenarios like data filtering, bifurcating datasets, or separating items into distinct categories based on a condition. It simplifies the process of categorizing and segregating data, enhancing code readability and efficiency.

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass