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

216
Visualizações
Want to create possible multi hierarchy sub category with its parent sub category string array?

Suppose I have an arraylist Of objects like below

[
{id:1,parent:0,name:"test 1",subs:[3,4]},
{id:2,parent:0,name:"test 2",subs:[5,6]},
{id:3,parent:1,name:"test 3",subs:[7]},
{id:4,parent:1,name:"test 4",subs:[]},
{id:5,parent:2,name:"test 5",subs:[]},
{id:6,parent:2,name:"test 6",subs:[8]},
{id:7,parent:3,name:"test 7",subs:[]},
{id:8,parent:6,name:"test 8",subs:[]},
]

now I want to make string array of names with having possible subs name

if consider above array then output should be like below

[
"test 1",
"test 1 - test 3",
"test 1 - test 4",
"test 1 - test 3 - test 7",
"test 2",
"test 2 - test 5",
"test 2 - test 6",
"test 2 - test 6 - test 8"
]

Please help me with solution. Thank you in advance

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

0

This solution builds a tree and iterate the children.

const
    data = [{ id: 1, parent: 0, name: "test 1", subs: [3, 4] }, { id: 2, parent: 0, name: "test 2", subs: [5, 6] }, { id: 3, parent: 1, name: "test 3", subs: [7] }, { id: 4, parent: 1, name: "test 4", subs: [] }, { id: 5, parent: 2, name: "test 5", subs: [] }, { id: 6, parent: 2, name: "test 6", subs: [8] }, { id: 7, parent: 3, name: "test 7", subs: [] }, { id: 8, parent: 6, name: "test 8", subs: [] }],
    getTree = (data, root) => {
        const t = {};
        data.forEach(o => ((t[o.parent] ??= {}).children ??= []).push(Object.assign(t[o.id] ??= {}, o)));
        return t[root].children;
    },
    flat = p => o => (name => [
        name,
        ...(o.children || []).flatMap(flat(name))
    ])(p + (p && ' - ') + o.name),
    tree = getTree(data, 0),
    result = tree.flatMap(flat(''));

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

An approach by using subs and an object of references by id.

const
    data = [{ id: 1, parent: 0, name: "test 1", subs: [3, 4] }, { id: 2, parent: 0, name: "test 2", subs: [5, 6] }, { id: 3, parent: 1, name: "test 3", subs: [7] }, { id: 4, parent: 1, name: "test 4", subs: [] }, { id: 5, parent: 2, name: "test 5", subs: [] }, { id: 6, parent: 2, name: "test 6", subs: [8] }, { id: 7, parent: 3, name: "test 7", subs: [] }, { id: 8, parent: 6, name: "test 8", subs: [] }],
    root = [],
    ids = Object.fromEntries(data.map(o => [o.id, (o.parent|| root.push(o.id), o)])),
    flat = p => id => (name => [
        name,
        ...ids[id].subs.flatMap(flat(name))
    ])(p + (p && ' - ') + ids[id].name),
    result = root.flatMap(flat(''));

console.log(result);
.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