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

134
Views
Recursivly change object nested property name

I have object with nested fields than named subItem I need to change names of this field to children.

How can this functionality be neatly implemented, perhaps there are ways besides recursion?

let data = [
  {
    name: 'Alex',
    subItem: [
      {
        name: 'Den',
        subItem: [
          
        ]
      }
    ]
  }
]

function transformData = (data) => {
  return data.map(stat=>{
    
  })
}

console.log(transformData(data))

//shoud be

let data = [
  {
    name: 'Alex',
    children: [
      {
        name: 'Den',
        children: []
      }
    ]
  }
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Javascript already includes a recursive object transformer, JSON. stringify with replacer function:

let data = [
  {
    name: 'Alex',
    subItem: [
      {
        name: 'Den',
        subItem: [ {name: 'Foo', subItem: []} ],
      }
    ]
  }
]



JSON.stringify(data, function(_, v) {
    if (v.subItem) {
        v.children = v.subItem
        delete v.subItem
    }
    return v
})

console.log(data)

In the simple case like this, you can also just manipulate the JSON string directly:

newData = JSON.parse(
    JSON.stringify(data)
        .replaceAll(`"subItem":`, `"children":`))
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!