MCPcopy Create free account
hub / github.com/Autodesk/react-base-table / unflatten

Function unflatten

src/utils.js:73–99  ·  view source on GitHub ↗
(array, rootId = null, dataKey = 'id', parentKey = 'parentId')

Source from the content-addressed store, hash-verified

71}
72
73export function unflatten(array, rootId = null, dataKey = 'id', parentKey = 'parentId') {
74 const tree = [];
75 const childrenMap = {};
76
77 const length = array.length;
78 for (let i = 0; i < length; i++) {
79 const item = { ...array[i] };
80 const id = item[dataKey];
81 const parentId = item[parentKey];
82
83 if (Array.isArray(item.children)) {
84 childrenMap[id] = item.children.concat(childrenMap[id] || []);
85 } else if (!childrenMap[id]) {
86 childrenMap[id] = [];
87 }
88 item.children = childrenMap[id];
89
90 if (parentId !== undefined && parentId !== rootId) {
91 if (!childrenMap[parentId]) childrenMap[parentId] = [];
92 childrenMap[parentId].push(item);
93 } else {
94 tree.push(item);
95 }
96 }
97
98 return tree;
99}
100
101export function flattenOnKeys(tree, keys, depthMap = {}, dataKey = 'id') {
102 if (!keys || !keys.length) return tree;

Callers 9

components.jsFile · 0.85
frozen-rows.jsFile · 0.85
resizable.jsFile · 0.85
scrollbar.jsFile · 0.85
max-height.jsFile · 0.85
row-key.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected