I'm attempting to modify objects within nested map functions. I have an array of objects with nested data.
When I try to execute it this way, the structure of the data changes and I end up with an array of arrays. All I really need is to add a "level" property to each object. Is it achievable using 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>