https://github.com/seregpie/lodash.permutations
Calculates all possible permutations without repetition of a certain size.
https://github.com/seregpie/lodash.permutations
array collection combinatorics lodash mixin permutation
Last synced: 5 months ago
JSON representation
Calculates all possible permutations without repetition of a certain size.
- Host: GitHub
- URL: https://github.com/seregpie/lodash.permutations
- Owner: SeregPie
- License: mit
- Created: 2020-06-04T09:52:47.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-04T11:55:27.000Z (almost 5 years ago)
- Last Synced: 2024-12-04T00:33:25.649Z (6 months ago)
- Topics: array, collection, combinatorics, lodash, mixin, permutation
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lodash.permutations
`_.permutations(collection, n)`
Calculates all possible permutations without repetition 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
```shell
npm i lodash.permutations
```### ES module
```javascript
import 'lodash.permutations';
import _ from 'lodash';
```### Node
```javascript
require('lodash.permutations');
let _ = require('lodash');
```### browser
```html
```
## usage
```javascript
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.
```javascript
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.
```javascript
let permutations = _('abc').permutations(3).map(v => _.join(v, '')).value();
// => ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
```## see also
- [lodash.combinations](https://github.com/SeregPie/lodash.combinations)
- [lodash.multicombinations](https://github.com/SeregPie/lodash.multicombinations)
- [lodash.multipermutations](https://github.com/SeregPie/lodash.multipermutations)
- [lodash.product](https://github.com/SeregPie/lodash.product)