Package Exports
- lodash.permutations
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lodash.permutations) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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
import 'lodash.permutations';
import _ from 'lodash';
Node
require('lodash.permutations');
let _ = require('lodash');
browser
<script src="https://unpkg.com/lodash"></script>
<script src="https://unpkg.com/lodash.permutations"></script>
usage
let permutations = _.permutations([true, {a: 1}, null], 2);
// => [[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 = _.flatMap([2, 4, 6], (v, i, a) => _.permutations(a, i + 1));
// => [[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 = _('abc').permutations(3).map(v => _.join(v, '')).value();
// => ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']