3

I'm having trouble figuring out how to generate a combination of values.

Given:

const items = ['a', 'b', 'c', 'd', 'e'];

should generate:

[
    ['a', 'b', 'c'],
    ['a', 'b', 'd'],
    ['a', 'b', 'e'],
    ['a', 'c', 'd'],
    ['a', 'c', 'e'],
    ['a', 'd', 'e'],

    ['b', 'c', 'd'],
    ['b', 'c', 'e'],

    ['c', 'd', 'e']
]

It generates a unique combination for all the items in the array. Basically, the length of the array for each item is Math.round(items.length / 2).

Any help would be greatly appreciated.

AnsellC
  • 795
  • 3
  • 9
  • 17
  • Does this answer your question? [Generate permutations of JavaScript array](https://stackoverflow.com/questions/37579994/generate-permutations-of-javascript-array) – Dominik Matis Jul 11 '20 at 20:46
  • 1
    There are a lot of questions about permutations of arrays in JavaScript. Please show what research you've done into the subject and what attempts you've made based on that research. – Heretic Monkey Jul 11 '20 at 20:49
  • @HereticMonkey I did do some research before posting. This one is quite unique since it is not really a complete permutation but takes a fixed length array. Permutation would have the items to be of equal length with the primary array. tia – AnsellC Jul 11 '20 at 21:05
  • Demonstration of research *and attempts you've made to solve the issue yourself* should be in the question. You can [edit] your question to provide that information. Also, there are questions around generating permutations within a certain length. [Generating all permutations of a certain length](https://stackoverflow.com/q/35323412/215552), for instance. Or [javascript permutation generator with permutation length parameter](https://stackoverflow.com/q/23305747/215552) – Heretic Monkey Jul 11 '20 at 21:12
  • Does this answer your question? [javascript permutation generator with permutation length parameter](https://stackoverflow.com/questions/23305747/javascript-permutation-generator-with-permutation-length-parameter) – Heretic Monkey Jul 11 '20 at 21:24
  • @HereticMonkey I'll take that in mind if ever I get to post a question again. My problem has been solved by the answer below. Thanks! – AnsellC Jul 11 '20 at 21:35

1 Answers1

5

You could take a straight forward approach and iterate the array and get the parts of the rest array by respecting the wanted length.

function perm(array, length) {
    return array.flatMap((v, i) => length > 1
        ? perm(array.slice(i + 1), length - 1).map(w => [v, ...w])
        : [[v]]
    );
}

perm(['a', 'b', 'c', 'd', 'e'], 3).forEach(a => console.log(...a));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thanks! Though the last item is a string and gets destructured. eg if its `const items =['how', 'are', 'you']` the result becomes `['how, 'are', 'y', 'o', 'u']`. It solves the main problem though and I can modify it to solve my problem. – AnsellC Jul 11 '20 at 21:34
  • The question is a duplicate, and should not have been answered. – Heretic Monkey Jul 11 '20 at 21:36
  • 1
    @AnsellC, now it does not spread the last single item. – Nina Scholz Jul 11 '20 at 21:47