Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including lodash.combinations with all npm packages installed. Try it out:

require("lodash/package.json"); // lodash is a peer dependency. var lodashCombinations = require("lodash.combinations")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

lodash.combinations v18.9.19

Calculates all possible combinations of a certain size.

lodash.combinations

_.combinations(collection, n)

Calculates all possible combinations of a certain size.

argumentdescription
collectionA collection of distinct values to calculate the combinations from.
nThe 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']]

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free