function createTree({
data,
key = 'id',
parentKey = 'parentId',
childrenKey = 'children',
}) {
const tree = Object.fromEntries(data.map(n => [ n[key], { ...n } ]));
return Object.fromEntries(Object.entries(tree).filter(([ , n ]) => !(
tree[n[parentKey]] && ((tree[n[parentKey]][childrenKey] ??= {})[n[key]] = n)
)));
}
const tree = createTree({
data: arr,
key: 'uid',
parentKey: 'parentUID',
});