Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
Array combinatorics in javascript, It's wrong?

I have created this script due to the need to perform unique combinations when I receive n number of arrays. It works, however when I want to store in a final array it writes something different.

let options = {
    "A": ["A1", "A2", "A3", "A4"],
    "B": ["B1", "B2"],
    "C": ["C1", "C2", "C3", "C4", "C5"],
};

let keys = Object.getOwnPropertyNames(options);
let size = keys.length;

let final = [];

function combinate(n, o) {

    for (let i = 0; i < options[keys[n]].length; i++) {

        if (n === (size - 1)) {
            o = {};
        }

        o[keys[n]] = options[keys[n]][i];

        if ((n - 1) >= 0) {
            combinate(n - 1, o);
        }

        if (n === 0) {
            console.log(o);
            final.push(o);
        }

    }

}

if (size > 0) combinate(size - 1, {});

console.log("-----------------------------------")
console.log(final);

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You're saving a reference to the same object multiple times in your array, and making changes to that object. You need to place a new copy of the object into the array.

Change: final.push(o); to final.push(Object.assign({}, o));

Or, more directly: final.push({A:o.A, B:o.B, C:o.C});

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to pass the reference of the object into the function and then copy the modified object to the final array

let options = {
    "A": ["A1", "A2", "A3", "A4"],
    "B": ["B1", "B2"],
    "C": ["C1", "C2", "C3", "C4", "C5"], };

let keys = Object.getOwnPropertyNames(options); 
let size = keys.length;

let final = [];

function combinate(n, o, fooFinal) {

    for (let i = 0; i < options[keys[n]].length; i++) {

        if (n === (size - 1)) {
            o = {};
        }

        o[keys[n]] = options[keys[n]][i];

        if ((n - 1) >= 0) {
            combinate(n - 1, o, fooFinal);
        }

        if (n === 0) {
            console.log(o);
            fooFinal.push(o);
        }

    }
    if(n===size-1){
      return fooFinal;
    }

}

if (size > 0){
  final = combinate(size - 1, {},[]);
}

console.log("-----------------------------------") 
console.log(final);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda