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

175
Visualizações
Traversing a javascript tree array of objects to compare it with another tree array of objects?

I have two trees of object type stated as follows:

export class asmTreeNode {
    uuid: uuidv4;
    name: string;
    instanceName: string;
    guid: string;
    componentId: number;
    type: string;
    transform: any = [];
    data: any;
    children: asmTreeNode[];
}

So, what I am supposed to do is compare if tree A (of object type asmTreeNode[]) with tree B (of object type asmTreeNode[]). And if a node is identical in tree B (the node is identical if the uuid matches) to tree A then it's properties are copied to the node in tree A. I have been told to start from the root node to do this and then move to the children (if parent nodes don't match then not to check the children). This is what I did so far but it's not giving the correct results I feel like there's something widely off with my logic.

 public compareAndCopyNodeProperties() {
        A: asmTreeNode [] = this.getExistingTree()
        B: asmTreeNode[] = this.getNewTree()

        var q1 = [];//queue to store the nodes of A that are to be checked
        var q2 = [];//queue to store the nodes of B that are to be checked
        var l = [];// List of identical nodes of A after it's property data has been upgraded from B

        A.forEach(e => {
            for (let i = 0; i < B.length; i++) {
                q1.push(e);
                q2.push(B[i])
                if (q1[i].uuid != q2[i].uuid) {
                    q1.pop();
                    q2.pop();
                    return;
                }

                else {
                    q1[i].data = q2[i].data;
                    l.push(q1);
                    q1.pop();
                    q2.pop();
                }
                e.children;
                B[i].children;
            }
        })

    }

Any input on how to correctly do this and procced ahead would be appreciated.:)

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

0

We will use a recursion for sure. The question is whether to compare children of same level, or of the entire tree. Anyway this is pretty basic example but maybe it will help.

var A = {
  uuid: 1,
  name: 'one',
  children: [{
      uuid: 2,
      name: 'two'
    },
    {
      uuid: 3,
      name: 'three',
      children: [{
        uuid: 4,
        name: 'four'
      }]
    }
  ]
}

var B = {
  uuid: 1,
  name: 'five',
  children: [{
      uuid: 2,
      name: 'same'
    },
    {
      uuid: 6,
      name: 'differ',
      children: [{
        uuid: 4,
        name: 'same again'
      }]
    }
  ]
}

function copy_matched_uuid_of_obj(A, B) {

  // if parent nodes don't match no need to check the children
  if (A.uuid != B.uuid) {
    return;
  }

  // copy properties, here only name
  A.name = B.name
    

  // compare for children (on same level!!!)
  if (A.children && B.children) {
    for (var i = 0; i < A.children.length; i++) {
      for (var j = 0; j < B.children.length; j++) {
        copy_matched_uuid_of_obj(A.children[i], B.children[j]);
      }
    }
  }
  
}

copy_matched_uuid_of_obj(A, B)
console.log(A)
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

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