亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用node js將字符串路徑轉換為JSON父子樹?

如何使用node js將字符串路徑轉換為JSON父子樹?

侃侃爾雅 2023-07-06 15:17:12
我一直在嘗試使用將路徑數組轉換為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"? ?}]知道如何產生上述輸出嗎?
查看完整描述

1 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

始終優先考慮可讀性而不是花哨:


const arr = [{

    "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"

  },

  {

    "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": "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"

  }

]


const tree = {

  name: 'root',

  path: '',

  children: []

}


for (const e of arr) {

  let node = tree

  const nodenames = e.fsLocation.split('/')

  while (nodenames.length > 0) {

    const nodename = nodenames.shift()

    if (!node.children.map(e => e.name).includes(nodename)) {

      node.children.push({

        name: nodename,

        path: [node.path, nodename].join('/'),

        children: []

      })

    }

    node = node.children.filter(e => e.name === nodename)[0]

  }

}

console.log(JSON.stringify(tree, null, 2));

返回樹:


{

  "name": "root",

  "path": "",

  "children": [

    {

      "name": "Events",

      "path": "/Events",

      "children": [

        {

          "name": "Plays",

          "path": "/Events/Plays",

          "children": [

            {

              "name": "Technologies",

              "path": "/Events/Plays/Technologies",

              "children": [

                {

                  "name": "operations.pptx",

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

                  "children": []

                },

                {

                  "name": "Continuity",

                  "path": "/Events/Plays/Technologies/Continuity",

                  "children": [

                    {

                      "name": "technology.pptx",

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

                      "children": []

                    }

                  ]

                },

                {

                  "name": "Solution.pdf",

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

                  "children": []

                }

              ]

            }

          ]

        }

      ]

    }

  ]

}


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號