Thursday, October 9, 2025
HomeLanguagesJavascriptLodash _.partitionBy() Method

Lodash _.partitionBy() Method

The 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:

Javascript




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


Output:

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

Example 2:

Javascript




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


Output:

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

Example 3:

Javascript




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


Output:

Original Array :  [
 1, 2,  3,  2, -1, -1,
 5, 6, -6, -6, -7, -8,
 9, 9, 10
]
Partitioned Arrayy:  [
 [ 1, 2, 3, 2 ],
 [ -1, -1 ],
 [ 5, 6 ],
 [ -6, -6, -7, -8 ],
 [ 9, 9, 10 ]
]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32346 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11877 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11940 POSTS0 COMMENTS
Shaida Kate Naidoo
6835 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6789 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS