Graceful way to find combinations in JavaScript I'm looking for combinations, not permutations, and have found some really great examples ranging from using flatMap such as https://stackoverflow.com/a/54329187/669843

The result of ['a', 'b', 'c'] would be an array of arrays of 3 (a, b, and c). The result I'm looking for would also contain arrays of length 1 and 2.

Example:

['a']
['b']
['c']
['a', 'b']
['a', 'b', 'c']
['a', 'c']
...


I'm accomplishing the results with something like this but this seems extraordinarily awful and requires de-duplication.

for(let i = 0; tci &lt; combo.length; tci++) {
  let newcombo = [allitems[item]].concat(combo.slice(tci, combo.length));
  ...
  push newcombo;
You can write this better.  The best way to explain it is to see the code example in Python's itertools module:

def combinations(iterable, r):
    # combinations('ABCD', 2) --&gt; AB AC AD BC BD CD
    # combinations(range(4), 3) --&gt; 012 013 023 123
    pool = tuple(iterable)
    n = len(pool)
    if r &gt; n:
        return
    indices = list(range(r))
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)


This generates the combinations of length 'r' efficiently using counting.  You can create all combinations by either adding length-1 'nulls' to the pool or just iterating over the lengths.

Combination generation is a bit hard to grok, and really you should step through this example until you get it.  It's a variation on the idea of manually counting used throughout many algorithms.  

The base idea for combinations('ABCD', 2) is to start with 'AB' (first yield), increment the last letter 'AC' then 'AD' with the for j loop doing nothing, on the next loop i will be the index 0, so 'A' -> 'B' and the second resets to one more than the first character and you yield 'BC'.

Trace the code.  Keep hacking!  Keep notes.