I'm trying to build/find a function, which will give me all combinations for N number of elements.
The solution below gives me an answer for pairs (i.e. 2 elements).
I'd like to parameterize it, so that I can define the number of combined elements (e.g. 3 elements => ['one', 'two', 'three'], ['one', 'two', 'four'], ... , 4 elements, and so on.
(Bonus internet points if you can tell me the name what I'm looking for (cartesian-product?)!)
var array = ['one', 'two', 'three', 'four', 'five'] // get pairs var result = array => array.flatMap((v, i) => array.slice(i+1).map( w => [v, w] )); console.log(result(array)) // output: // [ // ["one", "two"], // ["one", "three"], // ["one", "four"], // ["one", "five"], // ["two", "three"], // ["two", "four"], // ["two", "five"], // ["three", "four"], // ["three", "five"], // ["four", "five"] // ]
.filterbylength.