2017-08-22 6 views
1

Pythonのitertoolsと同じライブラリがJavaScriptにありますか?私は順列と組み合わせに特に興味があります。Itertools.combinations in Javascript

私はNode.jsを使用していません。

は、私はこのような何かをしたい:

array = ['a', 'b', 'c', 'd']; 

//return non-duplicate combinations of length 2 
['a', 'b'] 
['a', 'c'] 
['a', 'd'] 
['b', 'c'] 
['b', 'd'] 
['c', 'd'] 

感謝を! :)

+0

あなたは具体的でした –

+0

@AndreiMatracaru [Lodash](https://lodash.com/)を見てしたいのでしょうか?あなたは例を見せてもらえますか? :)ありがとう! –

+0

check [this](https://stackoverflow.com/a/41616364/3551786) – Durga

答えて

2

指定したサイズの配列の置換をgettignするために再帰的アプローチを使用できます。

function getPermutations(array, size) { 
 

 
    function p(t, i) { 
 
     if (t.length === size) { 
 
      result.push(t); 
 
      return; 
 
     } 
 
     if (i + 1 > array.length) { 
 
      return; 
 
     } 
 
     p(t.concat(array[i]), i + 1); 
 
     p(t, i + 1); 
 
    } 
 

 
    var result = []; 
 
    p([], 0); 
 
    return result; 
 
} 
 

 
var array = ['a', 'b', 'c', 'd']; 
 

 
console.log(getPermutations(array, 2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

デカルト商品の場合、thisの具体例であるlodashを確認するとよいでしょう。機能はPythonのitertools.productに似ています。

+0

セキュリティのためにページにアクセスできません。あなたはどこか別の場所にそれを見せることができますか?ありがとう! :) –