Finding all combination sequences from array in JS
<p><em>Disclaimer: I know part of this question has been asked and answered here before and yes, they have helped me get to this point so far.</em></p>

<p>Let's say I have an array that contains 2 elements and I want to find ALL possible combinations that could be made up of these elements.  The order of the sets does not matter.</p>

<p><code>var myArr = ['a','b'];</code></p>

<p>Desired result</p>

<p><code>var result = [ [a], [a,b], [b], [b,a] ]</code></p>

<p>At first I thought I was looking for a power set however I do not want a <code>null</code> set and I want to treat <code>[a,b]</code> and <code>[b,a]</code> as unique sequences NOT as equals. More info about sets, null set, and equal sets <a href="http://sevko.io/articles/power-set-algorithms/" rel="nofollow">here</a>.</p>

<p>So far I have written this function that will recursively loop through my array, create new arrays for each possible combinations and push them into a results array.</p>

<pre><code>function getCombinations() {

    var myArr = ['a','b'],
    result = [];

    var recurFn = function(prefix, myArr) {

        for (var i = 0; i &lt; myArr.length; i++) {

            var newArray = prefix !== '' ? [prefix, myArr[i]] : [myArr[i]];
            result.push(newArray);
        recurFn(prefix + myArr[i], myArr.slice(i + 1));
        }
    }

    recurFn('', myArr);
    console.log(result); //[[a], [a,b], [b]]

}
</code></pre>

<p>Here is a <a href="https://jsfiddle.net/x31n7s12/1/" rel="nofollow">fiddle</a> with my above code.</p>

<p>Currently I am only returning 3 possible combinations <code>[a], [a,b], [b]</code>, how can I edit my code so that I am returning <code>[a], [a,b], [b], [b,a]</code>.</p>

<p>Thanks!</p>