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>