lodash.multicombinations

Calculates all possible multicombinations of a certain size.

Usage no npm install needed!

<script type="module">
  import lodashMulticombinations from 'https://cdn.skypack.dev/lodash.multicombinations';
</script>

README

lodash.multicombinations

_.multicombinations(collection, n)

Calculates all possible multicombinations of a certain size.

argument description
collection A collection of distinct values to calculate the multicombinations from.
n The number of values to combine.

Returns a new array.

setup

npm

npm i lodash.multicombinations

ES module

import 'lodash.multicombinations';
import _ from 'lodash';

Node

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

browser

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

usage

let multicombinations = _.multicombinations([0, 1], 3);
// => [[0, 0, 0], [0, 0, 1], [0, 1, 1], [1, 1, 1]]

Also accepts array-like values.

let multicombinations = _('abcde').multicombinations(2).map(v => _.join(v, '')).value();
// => ['aa', 'ab', 'ac', 'ad', 'ae', 'bb', 'bc', 'bd', 'be', 'cc', 'cd', 'ce', 'dd', 'de', 'ee']

see also