npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

lodash.combinations

18.9.19 • Public • Published

lodash.combinations

_.combinations(collection, n)

Calculates all possible combinations of a certain size.

argument description
collection A collection of distinct values to calculate the combinations from.
n The Number of values to combine.

Returns a new array of combinations.

dependencies

setup

npm

npm install lodash.combinations

ES module

import 'lodash.combinations';
import _ from 'lodash';

Node

require('lodash.combinations');
let _ = require('lodash');

browser

<script src="https://unpkg.com/lodash"></script>
<script src="https://unpkg.com/lodash.combinations"></script>

usage

let combinations = _.combinations([{a: 11}, {b: 22}, {c: 33}], 2);
// => [[{a: 11}, {b: 22}], [{a: 11}, {c: 33}], [{b: 22}, {c: 33}]]

To calculate all possible combinations of all possible sizes use this code.

let combinations = _.flatMap([1, 2, 3], (v, i, a) => _.combinations(a, i + 1));
// => [[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

Accepts also array-like values.

let combinations = _.combinations('abcde', 3);
// => [['a', 'b', 'c'], ['a', 'b', 'd'], ['a', 'b', 'e'], ['a', 'c', 'd'], ['a', 'c', 'e'], ['a', 'd', 'e'], ['b', 'c', 'd'], ['b', 'c', 'e'], ['b', 'd', 'e'], ['c', 'd', 'e']]

install

npm i lodash.combinations

Downloadsweekly downloads

235

version

18.9.19

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability