Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptUnderscore.js _.partitionBy() Method

Underscore.js _.partitionBy() Method

The_.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:

  • 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 underscore.js contribute library to be installed.

underscore.js contrib library can be installed using: 

npm install underscore-contrib –save

Example 1: In this example, we will create a partitioned array of even and odd elements using this method.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// 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: In this example, we will create a partitioned array of all identical elements.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// 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: In this example, we will create a partitioned array of all negative and positive numbers.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// 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 ]
]

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

Recent Comments