I have one array of nested objects. I want to edit each of them and add property "status" and value of this property can be "selected", "unselected" or "indent".
1. status: 'selected'
- his lvlScope array is not empty
- lvlScope array of all his children is not empty
2. status: 'unselected'
- his lvlScope array is empty
- lvlScope array of all his children is empty
3. status: 'indent'
- his lvlScope array is empty and at least one of his children has lvlScope array that is not empty
- his lvlScope array is not empty and at least one of his children has lvlScope array that is empty
- his lvlScope array is not empty and all his children have lvlScope array that are empty
all children means all nested children
Does anyone has idea how to do that?
let data = [{
id: 1,
name: 'level1',
lvlScope: [1, 2, 3],
lvl1Subs: []
},
{
id: 2,
name: 'level1',
lvlScope: [],
lvl1Subs: [{
id: 1,
name: 'level2',
lvlScope: [],
lvl2Subs: [{
id: 1,
name: 'level3',
lvlScope: [],
lvl3Subs: []
},
{
id: 2,
name: 'level3',
lvlScope: [1, 2],
lvl3Subs: []
},
{
id: 3,
name: 'level3',
lvlScope: [1],
lvl3Subs: [{
id: 1,
name: 'level4',
lvlScope: [],
lvl4Subs: [{
id: 1,
name: 'level5',
lvlScope: []
},
{
id: 2,
name: 'level5',
lvlScope: [1]
}
]
},
{
id: 2,
name: 'level4',
lvlScope: [],
lvl4Subs: []
}
]
}
]
},
{
id: 2,
name: 'level2',
lvlScope: [1],
lvl2Subs: []
}
]
},
{
id: 3,
name: 'level1',
lvlScope: [],
lvl1Subs: [{
id: 1,
name: 'level2',
lvlScope: [1, 2],
lvl2Subs: []
},
{
id: 2,
name: 'level2',
lvlScope: [],
lvl2Subs: []
},
{
id: 3,
name: 'level2',
lvlScope: [1, 2, 3],
lvl2Subs: [{
id: 1,
name: 'level3',
lvlScope: [],
lvl3Subs: []
}]
},
{
id: 4,
name: 'level2',
lvlScope: [],
lvl2Subs: []
},
{
id: 5,
name: 'level2',
lvlScope: [1, 2],
lvl2Subs: []
}
]
}
]
const levels = (data) => {
data.map((lvl1) => {
console.log('-lvl1', lvl1)
lvl1.lvl1Subs.map((lvl2) => {
console.log('--lvl2', lvl2)
lvl2.lvl2Subs.map((lvl3) => {
console.log('---lvl3', lvl3)
lvl3.lvl3Subs.map((lvl4) => {
console.log('----lvl4', lvl4)
lvl4.lvl4Subs.map((lvl5) => {
console.log('-----lvl5', lvl5)
})
})
})
})
})
}
console.log(levels(data))