Public
Edited
Apr 25, 2023
1 fork
1 star
Insert cell
Insert cell
mutable source = 1
Insert cell
Insert cell
data = {
// const arr = [
// { name: "A", connectsTo: ["B", "C", "L"] },
// { name: "B", connectsTo: ["C", "D", "M"] },
// { name: "C", connectsTo: ["D", "E", "A", "B", "N"] },
// { name: "D", connectsTo: ["E", "F", "B"] },
// { name: "E", connectsTo: ["A", "B", "C", "D", "F", "G"] },
// { name: "F", connectsTo: ["D", "E", "G"] },
// { name: "G", connectsTo: ["B", "C"] },
// { name: "L", connectsTo: ["M", "W"] },
// { name: "M", connectsTo: [] },
// { name: "N", connectsTo: [] },
// ]
return new Map(
fiverrEzukutuDataSheet2.map((d) => [
d.id,
{
...d,
connectsTo: d.connectsTo
.split(",")
.filter((d) => d.trim())
.map((d) => +d)
}
])
);
}
Insert cell
getRoot = (source) => {
const node = data.get(source);

let root = {};
const visited = new Map();

if (node) {
root = { ...node };
visited.set(root.id, true);

if (node.connectsTo) {
root.children = node.connectsTo.map((d) => {
visited.set(d.id, true);

const n = data.get(d);

return {
...n,
children: []
};
});

root.children.forEach((d) => {
const n = data.get(d.id);

if (n) {
d.children = n.connectsTo
.filter((id) => {
return id !== source && id !== d.id && !visited.get(id);
})
.map((id) => {
visited.set(id, true);
const m = data.get(id);
return data.get(id) || {};
});
}
});
}
}

return root;
}
Insert cell
getRoot(1)
Insert cell
Insert cell
Fiverr ezukutu data - Sheet2.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
fiverrEzukutuDataSheet2
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more