0

I am stuck in the following code, i am getting all possible combination of layers from Photoshop. here in this example i have a group with three layers and (blue,purple,gold). I am taking these layers as array and the calling this combine function.

The problem is that every time a new combination is pushed in ""allCombination" array, the all combination array's previous element also turned to new element. In short if i had pushed [blue,purple] at allCombination[0] but when a push the second array combination as [blue,gold] in allCombination[1] The whole array "allCombination" become [blue,gold],[blue,gold]

var allCombinations = [];

var result = [];
result.length = 2; //n=2

function combine(input, len, start) {
  if(len === 0) {
    allCombinations.push(result); //process here the result
    $.writeln (allCombinations)
    return;
  }
  for (var i = start; i <= input.length - len; i++) {
    result[result.length - len] = input[i];
    combine(input, len-1, i+1 );
  }
}

var array = ["Blue", "purple", "gold"];    
  
combine( array, result.length, 0);

alert(allCombinations);

/////////////////////////////////

The above code gives following result

Blue,purple
Blue,gold,Blue,gold
purple,gold,purple,gold,purple,gold

0 Answers0