[{
"id": "market-executive-summary",
"title": "Executive Summary",
"interactivity": false,
"priceFactor": 2,
"children": [{
"id": "market-executive-summary-overview",
"title": "Overview",
"interactivity": false,
"priceFactor": 0,
"children": []
}, {
"id": "market-executive-summary-key-findings",
"title": "Key Findings",
"interactivity": false,
"priceFactor": 0,
"children": []
}, {
"id": "market-executive-summary-conclusions-implications",
"title": "Conclusions & Implications",
"interactivity": false,
"priceFactor": 0,
"children": []
}]
},
{
"id": "market-definitions-segmentation",
"title": "Definitions & Segmentation",
"interactivity": false,
"priceFactor": 2,
"children": [{
"id": "market-definitions-segmentation-definitions",
"title": "Definitions",
"interactivity": false,
"priceFactor": 0,
"children": []
}]
}
]id и isSelected: true | falseconst setProp = (arr, data) => {
const {id, isSelected } = data;
}const setProp = (arr, propData) => {
if (arr.length <= 1) return;
const { id, isSelected } = propData;
arr.forEach((element: any) => {
if (element[id]) {
element[id].isSelected = isSelected;
} else {
setProp(element.children, propData);
}
});
};
const id = "market-executive-summary-key-findings";
const isSelected = true;
const data = {
id, isSelected
}
setProp(arr, data)
const findInTree = (tree, childrenKey, test) =>
(Array.isArray(tree) ? tree : []).reduce((found, n) =>
found ?? (test(n) ? n : findInTree(n[childrenKey], childrenKey, test))
, null);const findInTree = function(tree, childrenKey, test) {
const stack = [];
for (let [ i, arr ] = this(tree); ++i < arr.length || stack.length;) {
if (i === arr.length) {
[ i, arr ] = stack.pop();
} else if (test(arr[i])) {
return arr[i];
} else {
stack.push([ i, arr ]);
[ i, arr ] = this(arr[i][childrenKey]);
}
}
return null;
}.bind(x => [ -1, x instanceof Array ? x : [] ]);const obj = findInTree(arr, 'children', n => n.id === id);
if (obj) {
obj.isSelected = isSelected;
}