如何使用node js将字符串路径转换为JSON父子树?

我一直在尝试使用将路径数组转换为JSON父子树node js

我正在使用我稍微修改过的提到的答案。下面是我的代码:

function buildTree(obj) {

  let result = [];

  let level = {

    result

  };


  obj.forEach(item => {

    if (typeof item.fsLocation != "undefined") {

      var obj = {}

      var path = ""

      item.fsLocation.split('/').reduce((r, name, i, a) => {

        path += "/"+name


        if (!r[name]) {

          r[name] = {

            result:[]

          };

          obj = {

            name,

            children: r[name].result

          }

          if(r[name].result.length < 1){

            obj["path"] = item.fsLocation

            obj["fileSize"] = item.fileSize

            obj["createDate"] = item.createDate

            obj["editDate"] = item.editDate

            obj["fileType"] = item.fileType

            obj["version"] = item.version

          }

          r.result.push(obj)

        }

        return r[name];

      }, level)

    }

  })

  return result

}

obj:


[

   {

      "createDate":"2019-10-03T07:00:00Z",

      "fileType":"pptx",

      "fsLocation":"Events/Plays/Technologies/Continuity/technology.pptx",

      "fileSize":46845322,

      "fileName":"technology.pptx",

      "editDate":"2019-10-03T07:00:00Z",

      "version":"10.0"

   },

   {

      "fileName":"operations.pptx",

      "fileSize":23642178,

      "fileType":"pptx",

      "fsLocation":"Events/Plays/Technologies/operations.pptx",

      "createDate":"2019-01-08T08:00:00Z",

      "editDate":"2019-01-09T08:00:00Z",

      "version":"15.0"

   },

   {

      "fileName":"Solution.pdf",

      "createDate":"2016-06-16T22:42:16Z",

      "fileSize":275138,

      "fsLocation":"Events/Plays/Technologies/Solution.pdf",

      "fileType":"pdf",

      "editDate":"2016-06-16T22:42:16Z",

      "version":"1.0"

   }

]

知道如何产生上述输出吗?



侃侃尔雅
浏览 90回答 1
1回答

芜湖不芜

始终优先考虑可读性而不是花哨:const arr = [{&nbsp; &nbsp; "fileName": "operations.pptx",&nbsp; &nbsp; "fileSize": 23642178,&nbsp; &nbsp; "fileType": "pptx",&nbsp; &nbsp; "fsLocation": "Events/Plays/Technologies/operations.pptx",&nbsp; &nbsp; "createDate": "2019-01-08T08:00:00Z",&nbsp; &nbsp; "editDate": "2019-01-09T08:00:00Z",&nbsp; &nbsp; "version": "15.0"&nbsp; },&nbsp; {&nbsp; &nbsp; "createDate": "2019-10-03T07:00:00Z",&nbsp; &nbsp; "fileType": "pptx",&nbsp; &nbsp; "fsLocation": "Events/Plays/Technologies/Continuity/technology.pptx",&nbsp; &nbsp; "fileSize": 46845322,&nbsp; &nbsp; "fileName": "technology.pptx",&nbsp; &nbsp; "editDate": "2019-10-03T07:00:00Z",&nbsp; &nbsp; "version": "10.0"&nbsp; },&nbsp; {&nbsp; &nbsp; "fileName": "Solution.pdf",&nbsp; &nbsp; "createDate": "2016-06-16T22:42:16Z",&nbsp; &nbsp; "fileSize": 275138,&nbsp; &nbsp; "fsLocation": "Events/Plays/Technologies/Solution.pdf",&nbsp; &nbsp; "fileType": "pdf",&nbsp; &nbsp; "editDate": "2016-06-16T22:42:16Z",&nbsp; &nbsp; "version": "1.0"&nbsp; }]const tree = {&nbsp; name: 'root',&nbsp; path: '',&nbsp; children: []}for (const e of arr) {&nbsp; let node = tree&nbsp; const nodenames = e.fsLocation.split('/')&nbsp; while (nodenames.length > 0) {&nbsp; &nbsp; const nodename = nodenames.shift()&nbsp; &nbsp; if (!node.children.map(e => e.name).includes(nodename)) {&nbsp; &nbsp; &nbsp; node.children.push({&nbsp; &nbsp; &nbsp; &nbsp; name: nodename,&nbsp; &nbsp; &nbsp; &nbsp; path: [node.path, nodename].join('/'),&nbsp; &nbsp; &nbsp; &nbsp; children: []&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; node = node.children.filter(e => e.name === nodename)[0]&nbsp; }}console.log(JSON.stringify(tree, null, 2));返回树:{&nbsp; "name": "root",&nbsp; "path": "",&nbsp; "children": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "name": "Events",&nbsp; &nbsp; &nbsp; "path": "/Events",&nbsp; &nbsp; &nbsp; "children": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "Plays",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "path": "/Events/Plays",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "Technologies",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "path": "/Events/Plays/Technologies",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "operations.pptx",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "path": "/Events/Plays/Technologies/operations.pptx",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children": []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "Continuity",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "path": "/Events/Plays/Technologies/Continuity",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "technology.pptx",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "path": "/Events/Plays/Technologies/Continuity/technology.pptx",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children": []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "Solution.pdf",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "path": "/Events/Plays/Technologies/Solution.pdf",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children": []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; ]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript