cildren
const copyTree = (tree, f) =>
tree.map(n => {
n = f instanceof Function ? f(n) : { ...n };
if (n.children instanceof Array) {
n.children = copyTree(n.children, f);
}
return n;
});
const dataCopy = copyTree(data, ({ name: key, ...x }) => ({ key, ...x }));
const traversal = (arr, prevKey, key) => arr.map((o) => {
const newObj = {
...o,
[key]: o[prevKey],
children: traversal(o.children, prevKey, key)
};
delete newObj[prevKey];
return newObj;
});
const newArr = traversal(data, 'name', 'key');
const flat = arr =>
(arr || []).reduce((acc, { children, ...n }) => {
if (n.isExpanded || !children) {
acc.push(n, ...flat(children));
} else {
acc.push({ ...n, children });
}
return acc;
}, []);
isExpanded: false
у кого-то из предков уровнем выше родительского, тоconst flat = arr =>
(arr || []).reduce((acc, { children, ...n }) => {
const flatChildren = flat(children);
if (n.isExpanded || !children) {
acc.push(n, ...flatChildren);
} else {
acc.push({ ...n, children: flatChildren });
}
return acc;
}, []);
<style>
.wraper{
width: 200px;
}
.blue{
width: 100%;
height: 50px;
background:blue;
}
.grey{
width: 50%;
height: 50px;
background: grey;
float:left;
}
.pink{
width: 50%;
height: 50px;
background: pink;
float:left;
}
</style>
<div class="wraper">
<div class="blue"></div>
<div class="grey"></div>
<div class="pink"></div>
<div style="clear:both"></div>
</div>