I have a problem with setting always unique index, incrementing by one.
I have an array like this.
const parentArr = [
{
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'
}
]
}
]
And I am expecting result like this:
const expectedResult = [
{
name: 'first parrent array',
additionalInfo: [
{
index: 0,
name: '1 - first child'
},
{
index: 1,
name: '1 - second child'
}
]
},
{
name: 'second parrent array',
additionalInfo: [
{
index: 2,
name: '2 -first child array'
},
{
index: 3,
name: '2- second child array'
}
]
}
]
I want to have index inside additionalInfo, for each item from 0.
I tried like this, but I got wrong index number
return parentArr.map((parent) => {
return {
name: parent.name,
additionalData: parent.childArray.map((child, index) => ({
name: child.name
index: index // I'M STUCK ON THIS LINE
})),
};
});