How to use the partition function from lodash

Find comprehensive JavaScript lodash.partition code examples handpicked from public code repositorys.

lodash.partition is a function that creates two arrays based on a given array and a predicate function, where one array contains values that passed the predicate function and the other contains values that didn't.

294
295
296
297
298
299
300
301
302
303
module.exports.padEnd              = _.padEnd;
module.exports.padStart            = _.padStart;
module.exports.parseInt            = _.parseInt;
module.exports.partial             = _.partial;
module.exports.partialRight        = _.partialRight;
module.exports.partition           = _.partition;
module.exports.partitionBy         = _.partitionBy;
module.exports.pick                = _.pick;
module.exports.pickBy              = _.pickBy;
module.exports.pickWhen            = _.pickWhen;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

251
252
253
254
255
256
257
258
259
260
261
262
263
console.log(map); // => [16, 64]


const orderBy = _.orderBy(['a', 'b', 'c'], ['c', 'a'], ['desc', 'asc']);
console.log(orderBy); // => ['c', 'a', 'b']


const partition = _.partition([1, 2, 3], n => n % 2);
console.log(partition); // => [[1, 3], [2]]


const reduce = _.reduce([1, 2], (sum, n) => sum + n, 0);
console.log(reduce); // => 3
fork icon0
star icon4
watch icon0

+ 15 other calls in file

How does lodash.partition work?

lodash.partition is a function that takes an array and a predicate function as inputs and returns two arrays: one with elements that satisfy the predicate and one with elements that don't satisfy the predicate.

Here's how it works: for each element in the input array, the predicate function is called with the element as its argument. If the predicate function returns true, the element is added to the first array; otherwise, it is added to the second array. The two arrays are returned as a tuple.

337
338
339
340
341
342
343
344
345
346
  orphans = false,
  supportSets = false
) {
  const { ADD, DELETE, SET } = diff(original, modified, orphans);
  const [_DELETE, _REMOVE] = supportSets
    ? _.partition(
        DELETE,
        (node) =>
          regex.isNumericSubscript$.test(node.path) &&
          (_.isNumber(node.value) || _.isString(node.value)) // @TODO: Support Node Buffer and/or ArrayBuffer sets?
fork icon0
star icon0
watch icon0

+ 8 other calls in file

10
11
12
13
14
15
16
17
18
19
20
21
  { 'user': 'pebbles', 'age': 1,  'active': false }
];


// lodash
console.log("lod.partition(users, function(o) { return o.active; })", lod.partition(users, function(o) { return o.active; }));
console.log("lod.partition(users, { 'age': 1, 'active': false })", lod.partition(users, { 'age': 1, 'active': false }));
console.log("lod.partition(users, ['active', false])", lod.partition(users, ['active', false]));


console.log('-----------------------------------------------');

fork icon0
star icon0
watch icon0

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
const _ = require("lodash");

const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];

const isEven = (num) => num % 2 === 0;

const [evens, odds] = _.partition(nums, isEven);

console.log(evens); // [2, 4, 6, 8]
console.log(odds); // [1, 3, 5, 7, 9]

In this example, lodash.partition is used to split an array of numbers into two arrays: one containing even numbers, and one containing odd numbers. The isEven function is used as the partitioning criteria, and it determines whether a number is even or odd. The resulting arrays are then logged to the console.

1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
var baseTxpSize = txp.getEstimatedSize();
var baseTxpFee = baseTxpSize * txp.feePerKb / 1000.;
var sizePerInput = txp.getEstimatedSizeForSingleInput();
var feePerInput = sizePerInput * txp.feePerKb / 1000.;

var partitionedByAmount = _.partition(inputs, function(input) {
  return input.satoshis > feePerInput;
});

info.utxosBelowFee = partitionedByAmount[1].length;
fork icon0
star icon0
watch icon0

+ 3 other calls in file

204
205
206
207
208
209
210
211
212
213
214
215
216


const skip = (n, enumerator) => iterator(skipGen(n, enumerator)); // TODO: implement take as a stateful transformer/transducer for composability




function partition(collection, predicate, matchesKey = 'matches', rejectsKey = 'rejects', optional = true) {
    const [truthy, falsey] = _.partition(collection, predicate);
    let result = {};
    if (optional) {
        if (!_.isEmpty(truthy)) {
            result[matchesKey] = truthy;
fork icon0
star icon0
watch icon0

+ 2 other calls in file

138
139
140
141
142
143
144
145
146
147
148
149


// partitionby


// users2 = _.partition(users, o => o.active);
// log(users2); // partion of active users and inactive users;
// users2 = _.partition(users, { active : true});
// log(users2);
// users2 = _.partition(users, ['active', false]);
// log(users2);

fork icon0
star icon0
watch icon0

+ 5 other calls in file

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)