partition - nodef/extra-lists GitHub Wiki

Segregate values by test result.

Alternatives: partition, partitionAs.

function partition(x, ft)
// x:  lists
// ft: test function (v, k, x)
const xlists = require('extra-lists');

var x = ['a', 'b', 'c', 'd'], [1, 2, 3, 4](/nodef/extra-lists/wiki/'a',-'b',-'c',-'d'],-[1,-2,-3,-4);
xlists.partition(x, v => v % 2 == 0).map(x => [...x]);
// → [ [ [ 'b', 'd' ], [ 2, 4 ] ], [ [ 'a', 'c' ], [ 1, 3 ] ] ]

var x = ['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5](/nodef/extra-lists/wiki/'a',-'b',-'c',-'d',-'e'],-[1,-2,-3,-4,-5);
xlists.partition(x, v => v % 2 == 1).map(x => [...x]);
// → [ [ [ 'a', 'c', 'e' ], [ 1, 3, 5 ] ], [ [ 'b', 'd' ], [ 2, 4 ] ] ]

References