there is a tree of categories. its an array of objects (in this case 1 ob
ject with children property. children property is an array containing
or not containing other categories objects and so on ). Each object also has a property “disabled”.
It can be either true or false. The starting point is that some bottommost children “disabled”
are set to true. In this case all of them are set to true. The task is to find parents ids
which disabled have to be set to true if all of its children disabled are set to true. In
this particular case all parents ids have to be found because all bottommost children disabled
are set to true. My function returns only lowest level of parents. What am I doing wrong?
let parentIdsToDisable = [];
function findParentIdsToDisable(tree) {
tree.forEach((category) => {
if (category.children.length > 0) {
if (category.children.every((child) => child.disabled === true)) {
category.disabled = true;
parentIdsToDisable.push(category.id);
}
}
findParentIdsToDisable(category.children);
});
}
const categories = [
{
id: 69,
name: 'Прикраси',
key: 'prykrasy',
description: 'Прикраси',
disabled: false,
mpath: '69.',
children: [
{
id: 70,
name: 'Аксесуари',
key: 'aksesyary-dlya-prykras',
description: 'Аксесуари для прикрас',
disabled: true,
mpath: '69.70.',
children: []
},
{
id: 72,
name: 'Ювелірні вироби',
key: 'uvelirni-vyroby',
description: 'Ювелірні вироби',
disabled: false,
mpath: '69.72.',
children: [
{
id: 73,
name: 'Срібло',
key: 'vyroby-iz-sribla',
description: 'Ювелірні вироби із срібла',
disabled: true,
mpath: '69.72.73.',
children: []
}
]
},
{
id: 71,
name: 'Біжутерія',
key: 'bizhuteriya',
description: 'Біжутерія',
disabled: true,
mpath: '69.71.',
children: []
}
]
}
]