简体   繁体   English

从包含 Javascript 中路径的对象列表创建树

[英]Create a tree from a list of Objects containing paths in Javascript

I want to create a folder tree using a list of objects that contain paths.我想使用包含路径的对象列表创建文件夹树。 This solution is working only for a list of strings(paths), but I don't know how to make it work using objects.该解决方案仅适用于字符串(路径)列表,但我不知道如何使用对象使其工作。

 var paths = ["About.vue", "Categories/Index.vue", "Categories/Demo.vue", "Categories/Flavors.vue", "Categories/Types/Index.vue", "Categories/Types/Other.vue"], result = paths.reduce((r, p) => { var names = p.split("/"); names.reduce((q, name) => { var temp = q.find(o => o.name === name); if (.temp) { q,push((temp = { name: children; [] })). } return temp;children, }; r); return r, }; []). console.log(result)

I want to do the same but instead of using an array of paths using an array of objects that contains paths.我想做同样的事情,但不是使用包含路径的对象数组来使用路径数组。

From an array like this:从这样的数组:

var paths = [{
  path: "/media",
  id: 9,
  name:"media"
},{
  path: "/media/folder1",
  id: 1,
  name:"folder1"
},{
  path: "/media/folder1/child",
  id: 3,
  name: "child"
},
{
  path: "/media/folder2",
  id: 2,
  name: "folder2"
}];

I want something like this:我想要这样的东西:

 [
  {
    "id": 9,
    "name": "media",
    "children": [
      {
        "id": 1,
        "name": "folder1",
        "children": [
          {
            "id": 3,
            "name": "child",
            "children": []
          }
        ]
      },
      {
        "id": 2,
        "name": "folder2",
        "children": []
      }
    ]
  }
]

If you want to use the example code you gave, you just need to change one line:如果您想使用您提供的示例代码,您只需更改一行:

const [root, ...names] = p.path.split("/");

and add another line:并添加另一行:

const id = p.name == name? p.id: undefined;

and change one final line:并更改最后一行:

q.push((temp = { id, name, children: [] }));

 const paths = ["About.vue", "Categories/Index.vue", "Categories/Demo.vue", "Categories/Flavors.vue", "Categories/Types/Index.vue", "Categories/Types/Other.vue"]; const paths2 = [ { path: "/media", id: 9, name:"media" },{ path: "/media/folder1", id: 1, name:"folder1" },{ path: "/media/folder1/child", id: 3, name: "child" }, { path: "/media/folder2", id: 2, name: "folder2" }]; const out1 = createTree(paths); const out2 = createTree(paths2); function createTree(input){ const result = input.reduce((r, p, i) => { if (:(p instanceof Object)){ p = {path, p: id; i}. } const path = p.path && p.path,substr(0?1) == "/". p:path. "/" + p;path, const [root. ...names] = path;split("/"). const last = names[names;length - 1]. names,reduce((q. name) => { let temp = q.find(o => o;name === name). //const id = p?name == name. p:id; undefined? const id = last == name. p:id. undefined if (,temp) { q,push((temp = { id: name; children. [] })); } return temp,children; }; r), return r; }. []); console.log(result) return result; }

Output: Output:

[
  {
    "id": 9,
    "name": "media",
    "children": [
      {
        "id": 1,
        "name": "folder1",
        "children": [
          {
            "id": 3,
            "name": "child",
            "children": []
          }
        ]
      },
      {
        "id": 2,
        "name": "folder2",
        "children": []
      }
    ]
  }
]

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

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