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

139
Views
How to update a recurring nested object property using its previous value using Javascript?

I got an array with some elements and each element has some properties like name, id and children. Each element has children property and then sub children so on and so forth. This array is already there in our application. Now I want to update all of elements and sub elements of our array with a new property called "path". The "path" will be the key and its value will be the addition of its previous object's name. For example

    let dataArray = [
      {
        name: 'Data',
        id: 12,
        path: 'Data',
        children: [
          {
            name: 'Data Center',
            id: 13,
            path: 'Data/Data Center',
            children: [
              {
                name: 'Data Management',
                id: 13,
                path: 'Data/Data Center/Data Managemet',
              },
            ],
          },
        ],
      },
      {
        name: 'Machine',
        id: 12,
        path: 'Machine',
        children: [
          {
            name: 'Machine Center',
            id: 13,
            path: 'Machine/Machine Center',
            children: [
              {
                name: 'Machine Management',
                id: 13,
                path: 'Machine/Machine Center/Machine Managemet',
              },
            ],
          },
        ],
      }
    ];

As you can see path property to all the elements. I want to make this happen programatically. For that, here is the piece of code I have written but I am not able to understand what need to need to be done to achieve. This piece of code adds path property to all the elements and sub elements but with name as path like above example. I want the addition of previous names which have been traversed.

    addPathProperty(){
    dataArray.forEach((element) =>{
        element.path = element.name;
        if(element.children.length > 0){
            this.addPathProperty(element.children)
        }
    });
}

Here is the link Stackblitz. Any help will be appreciated. Thank you

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think this code should work:

const appendPath = (root = '') => (obj) => {
  obj.path = root ? `${root}/${obj.name}` : obj.name

  if (obj.children) {
    obj.children.forEach(appendPath(obj.path))
  }
}

dataArray.forEach(appendPath())

appendPath is a function that accepts a root, when this function is invoked, it will combine root and name and set this to object.path; After that, it will do the same for the object.children if they exists

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!