lodash.permutations
_.permutations(collection, n)
Calculates all possible permutations of a certain size.
argument | description |
---|---|
collection |
A collection of distinct values to calculate the permutations from. |
n |
The number of values to combine. |
Returns a new array.
setup
npm
npm i lodash.permutations
ES module
;;
Node
;let _ = ;
browser
usage
let permutations = _;// => [[true, {a: 1}], [true, null], [{a: 1}, true], [{a: 1}, null], [null, true], [null, {a: 1}]]
Calculate all possible permutations of all possible sizes.
let permutations = _;// => [[2], [4], [6], [2, 4], [2, 6], [4, 2], [4, 6], [6, 2], [6, 4], [2, 4, 6], [2, 6, 4], [4, 2, 6], [4, 6, 2], [6, 2, 4], [6, 4, 2]]
Also accepts array-like values.
let permutations = value;// => ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']