简体   繁体   English

如何在javascript中将列表或数组显示到树结构中?

[英]How to show a list or array into a tree structure in javascript?

I pass this list from python to javascript like this: 我将此列表从python传递给javascript,如下所示:

 var string=["test_data/new_directory/ok.txt","test_data/reads_1.fq","test_data/test_ref.fa"];

I want output like this: 我想要这样的输出:

test_data
  reads_1.fq
  test_ref.fa
  new_directory
    ok.txt

Or also the output could be like this: 或者输出可能是这样的:

test_data
 reads_1.fq
 test_ref.fa

test_data/new_directory
  ok.txt

I used split function to get a list with each file and directory like this: 我使用split函数来获取每个文件和目录的列表,如下所示:

var string=["test_data/new_directory/ok.txt","test_data/reads_1.fq","test_data/test_ref.fa"];

                                     for(var i=0;i<string.length;i++){

                                         var result = string[i].split('/');

                                          console.log(result);


                                     }

Output looks like this: 输出如下:

["test_data", "new_directory", "ok.txt"]
["test_data", "reads_1.fq"]
["test_data", "test_ref.fa"]

How can I convert into the format I showed above? 我怎样才能转换成上面显示的格式? Thanks 谢谢

Sorry for being late to the party. 抱歉迟到了。 I ran into a similar issue trying to break out a list of paths into a nested object. 我遇到了类似的问题,试图将路径列表分解为嵌套对象。 Here's a fiddle showing how I ended up doing it. 这是一个小提琴,展示了我最终如何做到这一点。

var list = [];
list.push("A/B/C");
list.push("A/B/D");
list.push("A/B/C");
list.push("B/D/E");
list.push("D/B/E");
list.push("A/D/C");

var data = [];
for(var i = 0 ; i< list.length; i++)
{
   buildTree(list[i].split('/'),data);    
}
debugger;

function buildTree(parts,treeNode) {
     if(parts.length === 0)
     {
          return; 
     }

     for(var i = 0 ; i < treeNode.length; i++)
     {
          if(parts[0] == treeNode[i].text)
          {
              buildTree(parts.splice(1,parts.length),treeNode[i].children);
              return;
          }
     }

     var newNode = {'text': parts[0] ,'children':[]};
     treeNode.push(newNode);
     buildTree(parts.splice(1,parts.length),newNode.children);
}

https://jsfiddle.net/z07q8omt/ https://jsfiddle.net/z07q8omt/

That's certainly possible, but it requires recursion. 这当然是可能的,但它需要递归。

The first thing you'll want to do (as you've already figured out to do, in fact) is split on the slashes. 你要做的第一件事(事实上,你已经想到要做的事情)就是在斜线上分开。 We'll use map for simplicity: 为简单起见,我们将使用map

paths = paths.map(function(path) { return path.split('/'); });

Now we'll want to convert this into an array of objects with name and children properties. 现在我们要将它转换为具有namechildren属性的对象数组。 This means we'll have to use recursion. 这意味着我们必须使用递归。

In this function, we'll do a first pass grouping them by their first element: 在这个函数中,我们将通过第一个元素对它们进行第一次分组:

var items = [];
for(var i = 0, l = paths.length; i < l; i++) {
    var path = paths[i];
    var name = path[0];
    var rest = path.slice(1);
    var item = null;
    for(var j = 0, m = items.length; j < m; j++) {
        if(items[j].name === name) {
            item = items[j];
            break;
        }
    }
    if(item === null) {
        item = {name: name, children: []};
        items.push(item);
    }
    if(rest.length > 0) {
        item.children.push(rest);
    }
}

Then we can recurse on all of these (assuming the function name we chose was structurize ): 然后我们可以递归所有这些(假设我们选择的函数名是structurize ):

for(i = 0, l = items.length; i < l; i++) {
    item = items[i];
    item.children = structurize(item.children);
}

Now we've got a nice structure. 现在我们有一个很好的结构。 We can then stringify it, again with a recursive function. 然后我们可以使用递归函数对其进行字符串化。 Since the directory listing is just each item name followed by the indented directory contents listing, we can write that fairly easily: 由于目录列表只是每个项目名称后跟缩进目录内容列表,我们可以相当容易地写出:

function stringify(items) {
    var lines = [];
    for(var i = 0, l = items.length; i < l; i++) {
        var item = items[i];
        lines.push(item.name);
        var subLines = stringify(item.children);
        for(var j = 0, m = subLines.length; j < m; j++) {
            lines.push("  " + subLines[j]);
        }
    }
    return lines;
}

Then, to actually do it: 然后,实际做到这一点:

console.log(stringify(structurize(paths)).join("\n"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM