Skip to content
Advertisement

Converting csv file data to a JavaScript dictionary

CSV File Data:

JavaScript

Output: Javascript Object to be made

JavaScript

Basically, I have to convert a CSV file to make a Tree in d3. I am wondering how to make the dictionary required from the CSV file

JavaScript

So this is the code, I have which only make a dictionary-like, It doesn’t go deeper than the 1st level of the root node

JavaScript

Advertisement

Answer

Currently you’re only checking to see if the node matches the root (first) node. One idea might be to traverse the existing tree and check if the parent node you want to add already exists and then add a child to it. But this has some issues: it requires going back through the data, so it’s inefficient; and what happens if you encounter a parent_node that isn’t part of the tree yet? As vicatcu pointed out, a more efficient (and robust) way would be to create nodes as you encounter them and keep them in a dictionary/lookup object.

It could look something like this:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement