An Easy Way To Build A Tree in JavaScript Using Object References - TypeOfNaN
An Easy Way To Build A Tree in JavaScript Using Object References - TypeOfNaN
typeofnan
Introduction
Let’s say we have a tree data structure. This could be an organizational hierarchy, project
breakdown, animal/plant taxonomy, etc. The following is an example of a tree structure:
In an application, it would be fairly common to store this information in the following format,
especially if there is a 1-to-many parent/child node relationship:
https://typeofnan.dev/an-easy-way-to-build-a-tree-with-object-references/ 1/6
12/11/22, 8:48 PM An Easy Way to Build a Tree in JavaScript Using Object References | TypeOfNaN
const data = [
];
So how would we go from this array-of-objects format into a hierarchical tree format? This actually
becomes a fairly easy task when you take advantage JavaScript object references. It can be done
without recursion and in O(n) time.
To make sure we’re speaking the same language, let’s quickly go over some terminology I might
use. Each element in our array (i.e., each circle on our tree) is a “node.” A node can be a “parent”
of multiple nodes and a “child” of one node. In the picture above, node 86 is the “parent” of
node 80 and node 87 . node 86 is the “child” of node 74 . The top node of our tree is the “root.”
4. If there is no parent for an element, we know that will be our tree’s “root” element
https://typeofnan.dev/an-easy-way-to-build-a-tree-with-object-references/ 2/6
12/11/22, 8:48 PM An Easy Way to Build a Tree in JavaScript Using Object References | TypeOfNaN
We must realize that references will be maintained down the object tree, which is why we can
accomplish this in O(n) time!
acc[el.id] = i;
return acc;
}, {});
This mapping will come out as follows. You’ll soon see why this is helpful to have.
56: 0,
62: 7,
63: 4,
74: 2,
76: 3,
80: 5,
81: 1,
86: 8,
87: 6,
};
https://typeofnan.dev/an-easy-way-to-build-a-tree-with-object-references/ 3/6
12/11/22, 8:48 PM An Easy Way to Build a Tree in JavaScript Using Object References | TypeOfNaN
let root;
data.forEach((el) => {
root = el;
return;
// Use our mapping to locate the parent element in our data array
});
console.log(root);
id: 74,
parentId: null,
children: [
id: 62,
parentId: 74,
},
id: 86,
parentId: 74,
children: [
id: 80,
parentId: 86,
children: [{ id: 81, parentId: 80 }, { id: 76, parentId: 80 }],
},
https://typeofnan.dev/an-easy-way-to-build-a-tree-with-object-references/ 4/6
12/11/22, 8:48 PM An Easy Way to Build a Tree in JavaScript Using Object References | TypeOfNaN
],
},
],
};
If an object in memory has an array of children references, those children can have their own
growing array of children references. Since this is all done by reference, you don’t need to tell
parents anything when you’re modifying one of its children.
Conclusion
Object references is one of those foundational concepts in JavaScript that I believe can always
use more study and understanding. Really getting this concept can both help avoid tricky bugs and
provide for relative simply solutions to seemingly-complex problems.
If you'd like to support this blog by buying me a coffee I'd really appreciate it!
Subscribe to my newsletter
https://typeofnan.dev/an-easy-way-to-build-a-tree-with-object-references/ 5/6
12/11/22, 8:48 PM An Easy Way to Build a Tree in JavaScript Using Object References | TypeOfNaN
Join 2,498+ other developers and get free, weekly updates and code insights directly to your
inbox.
No spam
Unsubscribe whenever
EMAIL ADDRESS
Subscribe
Powered by Buttondown
https://typeofnan.dev/an-easy-way-to-build-a-tree-with-object-references/ 6/6