Javascript仅从一个数组中排除生成变体

时间:2016-06-24 21:03:46

标签: javascript

有一个带ID的数组。

var ingredientsIDs = new Array();
    ingredientsIDs[0]  = 1;
    ingredientsIDs[1]  = 2;
    ingredientsIDs[2]  = 3;
    ingredientsIDs[3]  = 4;

我试图找出如何将这些ID转换为另一个看起来像这样的数组。

  

数组[" .1"," .1.2"," .1.2.3"," .1.2.3.4" ," .2"," .2.3"," .2.3.4"," .3"," .3.4 "," .4"," .1.3"," .1.3.4" ," 1.4" ......等等]

我想得到所有"排列"但事实并非如此。 我浪费了一整天。

第一部分准备就绪,但我不知道如何继续。

到目前为止我写的是什么



var allVariation = new Array();
var index        = 0;
var n            = 0;
var l            = 0;
var i            = 0;
var j            = 0;

var ingredientsIDs = new Array();
ingredientsIDs[0]  = 1;
ingredientsIDs[1]  = 2;
ingredientsIDs[2]  = 3;
ingredientsIDs[3]  = 4;

for(i = 0; i <= ingredientsIDs.length; i++){
  for(j = 0; j < i; j++){
    if(allVariation[n] == undefined){allVariation[n] = "";}
    allVariation[n] += "." + ingredientsIDs[j];
  }
  if( i == ingredientsIDs.length  && l <= ingredientsIDs.length){
    l++;
    index = ingredientsIDs.indexOf(l);
    ingredientsIDs.splice(index, 1);
    i = 0;
  }
  n++;
}

console.log(allVariation);
&#13;
&#13;
&#13;

如果有人能告诉我某种方式,如何继续,我会很高兴。

0 个答案:

没有答案