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

148
Visualizações
How to generate Tree structure by parsing the string?

The original api response is as follows

 org = [
        { title: "New", count: 1 },
        { title: "Parent::Child::GrandChild", count: 0 },
        { title: "Parent::Child", count: 0 },
    ]

How can I convert a new object by parsing the title from the above response?

newObtj = [
            { title: "New", count: 1, child: [] },
            {
                title: "Parent",
                count: 0,
                child: [
                    { title: "Child", count: 0, child: [{ title: "GrandChild", count: 0, child: [] }] }
                ]
            },
            {
                title: "Parent",
                count: 0,
                child: [{ title: "Child", count: 0, child: [] }]
            },
        ]

Thank you in advance!

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

0

You should take the time to think through how you want your new object to be structured. To get you started, this is how I would take a delimited string and build an object from it:

   const treeString = 'Parent::Child::GrandChild'; // input string
    const treeParts = treeString.split('::');  // split into levels
    
    var tree = []; // will contain the final tree for this input
    var count = 0; // input count
    var child = []; // variable to hold children we have processed
 
    // loop through input from lowest level (GrandChild) up to top level (Parent)
    for (var i = treeParts.length - 1; i>=0; i--) 
    {
      // Create a child element (for top level this will be added to Tree)
      child = {
             title: treeParts[i], 
             count: count, 
             child: [ child ] 
        };
    } 
// Finally add to tree
tree.push(child);

You would probably want to turn this into a function and call it for each element in your original object.

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