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

128
Views
How can I get a new copy of an array of objects whose nested elements can grow indefinitely?

I am working with an array of objects that originally looks like this

const data = [
    {
        data: 1,
        children: [],
    },
    {
        data: 2,
        children: [],
    },
];

Eventually, these objects can grow indefinitely by filling the children's property with objects that share the shape of the original objects. For example

const data = [
    {
        data: 1,
        children: [
          {
              data: 3,
              children: [],
          },
          {
              data: 4,
              children: [],
          }
        ],
    },
    {
        data: 2,
        children: [],
    },
];

I need that when getting new data like this

[
    { data: 10, children: [] },
    { data: 12, children: [] },
]

and that must be placed in, for example, the children property of the object with the data property with value 4. How can I replace the entire array with the new values? As I mentioned before, nested objects can grow by filling the children's property indefinitely?

const data = [
    {
        data: 1,
        children: [
          {
              data: 3,
              children: [],
          },
          {
              data: 4,
              children: [
                  { data: 10, children: [] },
                  { data: 12, children: [] },
              ],
          }
        ],
    },
    {
        data: 2,
        children: [],
    },
];

Update 0: I need a new copy of the array with its updated objects because I work in angular and for a node tree component to be updated I need a new copy of values

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

0

You can have recursion to find data match and update children accordingly

const newData = [
    { data: 10, children: [] },
    { data: 12, children: [] },
]

const data = [
    {
        data: 1,
        children: [
          {
              data: 3,
              children: [],
          },
          {
              data: 4,
              children: [],
          }
        ],
    },
    {
        data: 2,
        children: [],
    },
];

function updateData(currentData, newData, dataValue) {
   if(currentData && Array.isArray(currentData) && currentData.length > 0) {
      for(let item of currentData) {
         //found item.data === 4
         if(item.data === dataValue) {
            //update children with your new data
            item.children = newData;
            break;
         }
         updateData(item.children, newData, dataValue)
      }
   }
}

//clone your data here
const clonedData = JSON.parse(JSON.stringify(data))

updateData(clonedData, newData, 4)

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

about 4 years ago · Juan Pablo Isaza Report

0

You can parse it using JSON (it won't catch functions though)

const data = [
    { data: 10, children: [] },
    { data: 12, children: [] },
];

const newObj = JSON.parse(JSON.stringify(data));

newObj[0].data = 15;
console.log(newObj[0].data); // 15
console.log(data[0].data); // 10

Or you can use lodash cloneDeep

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!