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

97
Views
Append array of objects by checking the nesting level: Javascript

I have an nested array of objects with the following format

const arr1 = [
  {
    name: "internalcorp.com",
    children: [
      {
        name: "internalcorp.com.child1",
        children: [
          {
            name: "internalcorp.com.grandchild1",
            children: []
          },
          {
            name: "internalcorp.com.grandchild2",
            children: []
          }
        ]
      },
      {
        name: "internalcorp.com.child2",
        children: []
      }
    ]
  },
  {
    name: "internalcorpwebsite.com",
    children: [
      {
        name: "internalcorpwebsite.com.child1",
        children: []
      }
    ]
  }
];

Need to add an className property to the each array object based on its level. Like for example, parent should have level-0 group and children items should have level-x leaf where x is the level number relative to the main parent.

Output should look like

const result = [
  {
    name: "internalcorp.com",
    className: "level-0 group",
    children: [
      {
        name: "internalcorp.com.child1",
        className: "level-1 leaf",
        children: [
          {
            name: "internalcorp.com.grandchild1",
            className: "level-2 leaf",
            children: []
          },
          {
            name: "internalcorp.com.grandchild2",
            className: "level-2 leaf",
            children: []
          }
        ]
      },
      {
        name: "internalcorp.com.child2",
        className: "level-1 leaf",
        children: []
      }
    ]
  },
  {
    name: "internalcorpwebsite.com",
    className: "level-0 group",
    children: [
      {
        name: "internalcorpwebsite.com.child1",
        className: "level-1 leaf",
        children: [],
      }
    ],
  }
];

Code that I have tried

const result = arr1.map((item,idx)=> {
  if(item.children.length){
    return {
      ...item,
      className: `level${idx} leaf`
    }
  }
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the power of recursion:

function recursive(item, id) {
  let returnedItem = item

  if (item.children.length) {
    returnedItem = {
        ...returnedItem,
        children: returnedItem.children.map(childItem => recursive(childItem, id + 1))
    }
  }

  if (0 === id) {
    return { ...returnedItem, className: `level-0 group` }
  }

  return { ...returnedItem, className: `level-${id} leaf` }
}

const result = arr1.map(item => recursive(item, 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!