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