I wrote the following recursive JS function that takes a single parameter node which is made of other nodes and modifies it by adding to each node its number of leaves (not only direct children) : Here’s an example of the node parameter : addWidth(object) would modify it and turn it into : I need help to make my function
Tag: tree
Build nodes like array from flat array
I’m trying to restructure the following javascript flat array without parent_id. I want to have a structure for React Tree Table like example in this page: https://www.robinwieruch.de/react-tree-table/ I want a another array that dynamically group according to this array’s keys: I want to have a structure like this and pass an another array to group. Ideally a want sum the
Getting all the children of a tree javascript object as a flat array
I have a tree-based javascript object. So, im trying to get all of them as a flat array, like this: What options do i have to get them all without doing some dirty spaguetti code? I tried using array.forEach() but it naturally requires doing another iteration inside it, and surely will require it again while doing in this way, Thanks
Javascript Building a Nested Object (tree) from an very weird array Debugging
I could really use some help Debugging a problem. Im trying to build a nested object (tree) from a very unusual array im getting from a backend (dont have access to the BE so i have to work with what i got). I’ve gotten 95 percent of it done but im running into a bug I know there are many
build array of object from tree object
I’m trying to get into javascript’s built-in reduce function and with the help of that build objects inside array. But you can use whatever function or method you want. Expected output What I tried to do I created a function which takes array as an argument and calls reduce for each array’s element. But it gives me output that I
Build tree array from flat ordered array when only “depth” and NOT “parent ID” is known
The answers in this thread and in other places I’ve searched only work if “parent_id” is known for each object in the array. What if the only property for the objects is depth? The depth essentially tells me if the item is a “root node” (depth of 1) or a “child node” (depth of 2 or more). A child’s parent
Transform Tree in Array to display with React Flow and Dagre
I have this tree: And I want to transform the data in this format so I can display with React-Flow (example here: https://reactflow.dev/examples/layouting/ This is the format I want: So ultimately I need to convert it to an array, get all the keys as id and get to find source and destination, based on parent/child structure. I would appreciate any
Recursion with map function in javascript
I have a map like this, which represents a graph: And I have this class, which creates a rooted tree: My goal is: given a root, I want to convert that graph to a rooted tree. For the example above, using 1 as root, I want the following return: Here’s my code: I’m receiving an empty array from the toRT
How to find path from tree of nodes
Consider a data structure similar to the one below: The function I have tried is as follows using recursion. I am getting an output: [{name: ‘Link10’, url: ‘link10’}] I would like to get an output as follows: And if I call the function as follows: Should return results as [{name: ‘Link11’, url: ‘link11’,}] I tried the following as well and
Print family names in a hierarchical tree structure
There is an array of data objects which have parent and child relations in it. I’d like to print the structure of the tree by level, printing only the names. If there is no parent information indicated then it is the root node. Sample input: Output: I tried using reduce to get the object structure but couldn’t get the traverse