Skip to content

partition

Subhajit Sahu edited this page Jun 17, 2020 · 13 revisions

Segregates values by test result. 🏃 📼 📦 🌔 📒

Alternatives: [partition], [partitionAs].

entries.partition(x, fn);
// x:  entries
// fn: test function (v, k, x)
// --> [satisfies, doesnt]
const entries = require('extra-entries');

var x = [['a', 1], ['b', 2], ['c', 3], ['d', 4]];
entries.partition(x, v => v % 2 == 0).map(x => [...x]);
// [ [ [ 'b', 2 ], [ 'd', 4 ] ], [ [ 'a', 1 ], [ 'c', 3 ] ] ]

var x = [['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]];
entries.partition(x, v => v % 2 == 1).map(x => [...x]);
// [ [ [ 'a', 1 ], [ 'c', 3 ], [ 'e', 5 ] ], [ [ 'b', 2 ], [ 'd', 4 ] ] ]

references

Clone this wiki locally