Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!