Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
Array Weird Results in Cartesian product javasccript

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
about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda