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

173
Views
Change objects in nested structure using map function

I am trying to change objects in nested map functions. I have one array of objects with nested data.

If I try to do on this way structure of data is changing. I'm getting array of arrays. Only what I need here is to add for each object one property "level". Is it possible to do this with nested map functions?

const res = data.map(itemA => ({ ...itemA,
    level: 'a'
  })
  .subA.map(itemB => ({ ...itemB,
    level: 'b'
  })))

console.log(res)
<script>
  let data = [{
      name: 'testA1',
      subA: [{
          name: 'testB1',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        },
        {
          name: 'testB2',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        }
      ]
    },
    {
      name: 'testA2',
      subA: [{
        name: 'testB1',
        subB: [{
          name: 'testC1',
          subC: [{
            name: 'testD1',
            subD: []
          }]
        }]
      }]
    }
  ]
</script>

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

0

You can use a recursive function?

let data = [{ name: 'testA1', subA: [{ name: 'testB1', subB: [{ name: 'testC1', subC: [] }] }, { name: 'testB2', subB: [{ name: 'testC1', subC: [] }] } ] }, { name: 'testA2', subA: [{ name: 'testB1', subB: [{ name: 'testC1', subC: [{ name: 'testD1', subD: [] }] }] }] } ]

function addprop(arr, level){
    arr.map(x=>{
        const match = Object.keys(x).find(element => {
            if (element.includes('sub')) {
              return true;
            }
          });
          addprop(x[match], level+1)
    })
    return arr.map(x=>(x.level=level,x))
}

console.log(addprop(data, 1))

about 4 years ago · Juan Pablo Isaza Report

0

Actually I did it on this way:

const res = data.map(itemA => (
  {...itemA, level: 'a', subA: itemA.subA.map(itemB => (
    {...itemB, level: 'b', subB: itemB.subB.map(itemC => (
      {...itemC, level: 'c'}
    ))}
  ))}
))
console.log(res)
<script>
  let data = [{
      name: 'testA1',
      subA: [{
          name: 'testB1',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        },
        {
          name: 'testB2',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        }
      ]
    },
    {
      name: 'testA2',
      subA: [{
        name: 'testB1',
        subB: [{
          name: 'testC1',
          subC: [{
            name: 'testD1',
            subD: []
          }]
        }]
      }]
    }
  ]
</script>

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!