Given the structure above, each type currently has text-1
.
I am looking to change only the first occurrence to text-1
, while the rest should be changed to text-2
in the data structure
.
The order of the blocks must remain the same and different types within a block should not be affected.
I have attempted to loop through each block, but I'm struggling with the logic to modify only the first occurrence. Please assist.
Below is my current attempt:
let newData = data.forEach(data => {
return data.block.forEach(block => {
return block.child.forEach((type, i) => {
if (i === 0) { return }
type.type = 'text-2'
})
});
});
Current Data
const data = [
{
block: [
{ child: [{ type: 'text-1' }] },
{ child: [{ type: 'any type' }] },
{ child: [{ type: 'text-1' }] }
]
},
{
block: [
{ child: [{ type: 'text-1' }] },
{ child: [{ type: 'text-1' }] }
]
}
]
Expected Data,
const data = [
{
block: [
{ child: [{ type: 'text-1' }] },
{ child: [{ type: 'any type' }] },
{ child: [{ type: 'text-2' }] }
]
},
{
block: [
{ child: [{ type: 'text-2' }] },
{ child: [{ type: 'text-2' }] }
]
}
]