0

There is an array with IDs.

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

I tried to find out how to convert these IDs to another array what looks like this.

Array [ ".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" .... etc]

I would like to get all "permutations" but it is not really that. I wasted the whole day.

The first part is ready but I don't known how to continue.

What I wrote so far

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);

So I would be happy if somebody can show me some way, how to continue.

Michael
  • 425
  • 3
  • 13

0 Answers0