title: Universal function for getting all unique pairs, trebles etc from an array in javascript
body: <p>I am looking to create a function in javascript, which would allow me to pass a long array, together with one argument.
  what I'm looking for is something like this:</p>
  
  <pre><code>var ar = [1,2,3,4];
  var pairs = superAwesomeFunction(ar,2) //=&gt; [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]];
  var trebles = superAwesomeFunction(ar,3) //=&gt; [[1,2,3],[1,2,4],[1,3,4],[2,3,4]
  </code></pre>
  
  <p>ideally, the function would have no limit on the folding argument. 
  I wrote a piece of code that looks like this, which is working fine, but it's not really useful as it isn't universal, and I would need a lot of such functions, which seems silly. </p>
  
  <pre><code>  getAll2Folds: function (ar) {
      var combinations = [],
          numOdds = ar.length;
      for (var i = 0; i &lt; numOdds; i++) {
        for (var j = i + 1; j &lt; numOdds; j++) {
          combinations.push([ar[i], ar[j]]);
        }
      }
      return combinations;
    },
  
    getAll3Folds: function (ar) {
      var combinations = [],
          numOdds = ar.length;
      for (var i = 0; i &lt; numOdds; i++) {
        for (var j = i + 1; j &lt; numOdds; j++) {
          for (var k = j + 1; k &lt; numOdds; k++) {
            combinations.push([ar[i], ar[j], ar[k]]);
          }
        }
      }
      return combinations;
      };
  
    },
  </code></pre>
  
  <p>... (not so great :|)</p>
  
  <pre><code>  getAll8Folds: function (ar) {
  var combinations = [],
      numOdds = ar.length;
  for (var i = 0; i &lt; numOdds; i++) {
    for (var j = i + 1; j &lt; numOdds; j++) {
      for (var k = j + 1; k &lt; numOdds; k++) {
        for (var l = k + 1; l &lt; numOdds; l++) {
          for (var m = l + 1; m &lt; numOdds; m++) {
            for (var n = m + 1; n &lt; numOdds; n++) {
              for (var o = n + 1; o &lt; numOdds; o++) {
                for (var p = o + 1; p &lt; numOdds; p++) {
                  combinations.push([ar[i], ar[j], ar[k], ar[l], ar[m], ar[n], ar[o], ar[p]]);
                }
              }
            }
          }
        }
      }
    }
  }
  return combinations;
  </code></pre>
  
  <p>}</p>
  
  <p>I'm free to use underscore, jquery or whatever tool i want, but can't find an elegant solution, which would also be performant. ideas? </p>
  
  <p>Thanks</p>