I have this data :
{
"id": 26578,
"label": "CatA",
"value": 26578,
"children": [
{
"id": 26579,
"label": "CatB",
"value": 26579,
"children": [
{
"id": 26580,
"label": "CatC",
"value": 26580,
"children": null
},
{
"id": 26581,
"label": "CatD",
"value": 26581,
"children": null
}
]
}
]
},
I know the value 26578
, I called chosenSubcategoryId
I want to get 2 arrays :
expanded = [26578, 26579]
checked = [26580, 26581]
I try like this :
const recursion = (item, chosenSubcategoryId) => {
if (Array.isArray(item)) {
item.map(item => {
if (item.id === chosenSubcategoryId) {
recursion(item.children, item.id);
}
})
}
}
recursion(this.props.chosenCategory.children, this.props.chosenSubcategory.id);
But not working properly. Please help me ! Thx in advance.
question from:
https://stackoverflow.com/questions/65845465/how-does-one-within-a-nested-object-structure-recursively-collect-different-ty 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…