Struggling with setting a consistently unique index that increments by one.
Here is an example of my array:
const originalArr = [
{
name: 'first parent array',
childArray: [
{
name: '1 / first child'
},
{
name: '1 / second child'
}
]
},
{
name: 'second parent array',
childArray: [
{
name: '2 / first child array'
},
{
name: '2 / second child array'
}
]
}
]
The desired output should look like this:
const expectedOutput = [
{
name: 'first parent array',
additionalInfo: [
{
index: 0,
name: '1 - first child'
},
{
index: 1,
name: '1 - second child'
}
]
},
{
name: 'second parent array',
additionalInfo: [
{
index: 2,
name: '2 - first child array'
},
{
index: 3,
name: '2 - second child array'
}
]
}
]
I need each item in additionalInfo to have a unique index starting from 0.
My attempt so far has resulted in incorrect index numbers:
return originalArr.map((parent) => {
return {
name: parent.name,
additionalData: parent.childArray.map((child, index) => ({
name: child.name,
index: index // Struggling to increment properly here
})),
};
});