Open In App

Lodash _.partitionBy() Method

Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.partitionBy() method takes an array and a function and hence generates a partitioned array based on the conditions of the given function.

Syntax:

_.partitionBy(array, function)

Parameters:

This method takes two parameters as mentioned above and described below:

  • array: The given array from which the partitioned array is to be created.
  • function: The function containing the conditions for an array to be partitioned.

Return Value:

This method returns a newly created partitioned array.

Note: This will not work in normal JavaScript because it requires the lodash.js contribute library to be installed. Lodash.js contrib library can be installed using npm install lodash-contrib –save.

Example 1: This code uses the Lodash Contrib library to partition an array into two subarrays based on whether each element is odd or even using the _.partitionBy() method and the _.isOdd() predicate function.

Javascript




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
// Declare an array
let array = [1, 2, 3, 2, 1, 1,
    5, 6, 6, 6, 7, 8, 9, 9, 10];
 
// Creating partitioned array
let p_arr = _.partitionBy(array, _.isOdd);
 
console.log("Original Array : ", array);
console.log("Partitioned Array: ", p_arr);


Output:

Original Array :  [
1, 2, 3, 2, 1, 1,
5, 6, 6, 6, 7, 8,
9, 9, 10
]
Partitioned Array: [
[ 1 ], [ 2 ],
[ 3 ], [ 2 ],
[ 1, 1, 5 ], [ 6, 6, 6 ],
[ 7 ], [ 8 ],
[ 9, 9 ], [ 10 ]
]

Example 2: The code partitions the given array into two subarrays based on whether each element is equal to itself using the _.partitionBy() method and the _.identity() predicate function.

Javascript




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
// Declare an array
let array = [1, 2, 3, 2, 1, 1,
    5, 6, 6, 6, 7, 8, 9, 9, 10];
 
// Creating partitioned array
let p_arr = _.partitionBy(array, _.identity);
 
console.log("Original Array : ", array);
console.log("Partitioned Array: ", p_arr);


Output:

Original Array :  [
1, 2, 3, 2, 1, 1,
5, 6, 6, 6, 7, 8,
9, 9, 10
]
Partitioned Array: [
[ 1 ], [ 2 ],
[ 3 ], [ 2 ],
[ 1, 1 ], [ 5 ],
[ 6, 6, 6 ], [ 7 ],
[ 8 ], [ 9, 9 ],
[ 10 ]
]

Example 3: This code partitions the given array into two subarrays based on whether each element is greater than 0 using the _.partitionBy() method and a custom predicate function.

Javascript




// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
// Declare an array
let array = [1, 2, 3, 2, -1, -1, 5,
        6, -6, -6, -7, -8, 9, 9, 10];
 
// Creating partitioned array
let p_arr = _.partitionBy(array, function(val) {
  return val > 0
});
 
console.log("Original Array : ", array);
console.log("Partitioned Array: ", p_arr);


Output:

Original Array :  [
1, 2, 3, 2, -1, -1,
5, 6, -6, -6, -7, -8,
9, 9, 10
]
Partitioned Array: [
[ 1, 2, 3, 2 ],
[ -1, -1 ],
[ 5, 6 ],
[ -6, -6, -7, -8 ],
[ 9, 9, 10 ]
]



Last Updated : 13 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads