I would like to id all the objects inside my tree array sequentially. To do this, I need to iterate all the array objects and objects' children, grandchildren etc.. and insert rowId to each.
What I have;
rows={[
{
car: 'Audi A4',
city: 'Las Vegas',
items: null,
name: 'Sandra',
sex: 'Female'
},
{
car: 'Chevrolet Cruze',
city: 'Tokyo',
items: [
{
car: 'BMW 750',
city: 'London',
items: [
{
car: 'Toyota Corolla',
city: 'Chicago',
name: 'David',
sex: 'Male'
},
],
name: 'Robert',
sex: 'Male'
}
],
name: 'Sharon',
sex: 'Female'
}
]}
What I expect;
rows={[
{
car: 'Audi A4',
city: 'Las Vegas',
items: null,
name: 'Sandra',
sex: 'Female',
rowId: 1
},
{
car: 'Chevrolet Cruze',
city: 'Tokyo',
items: [
{
car: 'BMW 750',
city: 'London',
items: [
{
car: 'Toyota Corolla',
city: 'Chicago',
name: 'David',
sex: 'Male',
rowId: 2
},
],
name: 'Robert',
sex: 'Male',
rowId: 3
}
],
name: 'Sharon',
sex: 'Female',
rowId: 4
}
]}
I could do this iteration like this when there is no inner objects;
rows.map((row, index) => ({ ...row, rowId: index }));
Also tried some recursive methods but none of them worked actually neither found something useful on the web.
Looking for a genius developer who can guide me through this. Thanks in advance.