Saturday, October 18, 2025
HomeLanguagesJavascriptLodash _.partition() Method

Lodash _.partition() Method

Lodash _.partition() method creates an array of elements split into two groups, the first of which contains elements predicate returns true and the second of which contains elements predicate returns false.

Syntax:

_.partition(collection, predicate);

Parameters:

  • collection (Array|Object) parameter holds the collection to iterate over.
  • predicate (Function) parameter holds the function invoked per iteration and is invoked with one argument (value).

Return Value:

This method returns the array of grouped elements.

Example 1: In this example, we are partitioning our array into two parts one which has true as an active value and another is false as an active value.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let users = [
    { 'customer': 'john', 'age': 26, 'active': false },
    { 'customer': 'jonny', 'age': 34, 'active': true },
    { 'customer': 'johnson', 'age': 12, 'active': false }
];
 
// Use of _.partition() method
 
let gfg = _.partition(users,
    function (o) { return o.active; });
 
// Printing the output
console.log(gfg);


Output:

[
  [ { customer: 'jonny', age: 34, active: true } ],
  [
    { customer: 'john', age: 26, active: false },
    { customer: 'johnson', age: 12, active: false }
  ]
]

Example 2: In this example, we are partitioning our array into two parts. we are passing our condition as an object into the _.partitiion() method.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let users = [
    { 'customer': 'john', 'age': 26, 'active': false },
    { 'customer': 'jonny', 'age': 34, 'active': true },
    { 'customer': 'johnson', 'age': 12, 'active': false }
];
 
// Use of _.partition() method
// The `_.matches` iteratee shorthand
 
let gfg = _.partition(users, {
    'age': 12, 'active': false
});
 
// Printing the output
console.log(gfg);


Output:

[
  [ { customer: 'johnson', age: 12, active: false } ],
  [
    { customer: 'john', age: 26, active: false },
    { customer: 'jonny', age: 34, active: true }
  ]
]

Example 3: In this example, we are partitioning our array into two parts. we are passing our condition as an array into the _.partitiion() method.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let users = [
    { 'customer': 'john', 'age': 26, 'active': false },
    { 'customer': 'jonny', 'age': 34, 'active': true },
    { 'customer': 'johnson', 'age': 12, 'active': false }
];
 
// Use of _.partition() method
// The `_.matchesProperty` iteratee shorthand
 
let gfg = _.partition(users, ['active', false]);
 
// Printing the output
console.log(gfg);


Output:

[
  [
    { customer: 'john', age: 26, active: false },
    { customer: 'johnson', age: 12, active: false }
  ],
  [ { customer: 'jonny', age: 34, active: true } ]
]
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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS