how to encode this data to parent / children structure in JSON

前端 未结 3 1804
灰色年华
灰色年华 2021-02-02 14:44

I am working with d3.js to visualise families of animals (organisms) (up to 4000 at a time) as a tree graph, though the data source could just as well be a directory listing, or

3条回答
  •  耶瑟儿~
    2021-02-02 15:16

    The following is specific to the structure you've provided, it could be made more generic fairly easily. I'm sure the addChild function can be simplified. Hopefully the comments are helpful.

    function toHeirarchy(obj) {
    
      // Get the organisms array
      var orgName, orgNames = obj.organisms;
    
      // Make root object
      var root = {name:'ROOT', children:[]};
    
      // For each organism, get the name parts
      for (var i=0, iLen=orgNames.length; i

提交回复
热议问题