【问题标题】:Create nested object of file paths创建文件路径的嵌套对象
【发布时间】:2018-01-28 12:41:06
【问题描述】:

我正在递归地遍历大型目录树(从 100,000 到 1,000,000 个对象),并且需要将每个文件或目录添加到深度嵌套的对象中。

假设我有类似的文件路径

/path/to/file1
/path/to/file2
...
/pathX/file1000000

我需要基于它们创建以下嵌套对象:

{
  "name": "/",
  "value": 300,
  "children": [
    { 
       "name": "/pathX",
       "value": 100,
       "children": [
       {
         "name": "/pathX/file1000000",
         "value": 100
       }
    },
    {
      "name": "/path",
      "value": 200,
      "children": [
        {
          "name": "/path/to",
          "value": 200,
          "children": [
            {
              "name": "/path/to/file1",
              "value": 100
            },

            {
              "name": "/path/to/file2",
              "value": 100
            }
          ]
        }
      ]
    }
  ]
}

value 是文件大小或嵌套文件大小的总和。为简单起见,假设file1file2fileN 等于100

我能够为一个文件构建嵌套对象,但在为具有不同路径的许多文件构建它时遇到问题:

const path = require('path')
const fs = require('fs')

let file = '/opt/bin/file1'
let size
fs.stat(file, (err, stats) => { size = stats.size })
let paths = file.split(path.sep)

let nameChild = file
let objChild = { "name" : nameChild, "value" : size }
let nameParent
let objParent

for (var i in paths) {

    if (i==0) continue

    nameParent = path.dirname(nameChild)

    objParent = { "name" : nameParent, "value" : size, "children" : [ objChild ] }

    nameChild = nameParent
    objChild = objParent

}

console.log(JSON.stringify(objParent))

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    你去吧:)

    function buildTree(pathes, getValueCB) {
    
      var currentPath, lastPath, node, parent, map = {
          "": {
            children: []
          }
        },
        stack = [""]
    
      for (let path of pathes) {
        let nodes = path.split("/");
        for (let i = 0; i < nodes.length; i++) {
          currentPath = "/" + nodes.slice(1, i + 1).join("/")
          lastPath = stack[stack.length - 1]
          parent = map[lastPath]
          if (!map[currentPath]) {
            node = {
              name: currentPath,
              value: getValueCB(currentPath),
              children: []
            }
            parent.children.push(node);
            map[currentPath] = node;
          }
          stack.push(currentPath)
        }
        stack = stack.slice(0, 1)
      }
      return map[""].children[0];
    }
    
    function getFileSizeSync() {
      return 200
    }
    var tree = buildTree(["/path/to/file1", "/path/to/file2"], function(path) {
      return getFileSizeSync(path)
    })
    
    console.log (tree)

    这是递归计算大小的更新版本。 (我不能把它放进一个sn-p,这就是我离开旧的原因)

    var fs = require('fs')
    var Path = require ('path')
    
    function calcSize (node) {
        var children = node.children; 
        node.value = children.reduce (function (size, child) {
            return size + child.value || reduceSize (child);
        }, 0)
        return node.value;
    }
    function getFileSizeSync(path) {
      var size = fs.statSync(path).size
      return size
    }
    
    function buildTree(pathes, getValueCB) {
    
      var currentPath, lastPath, node, parent, map = {
          "": {
            children: []
          }
        },
        stack = [""]
    
      for (let path of pathes) {
        let nodes = path.split(Path.sep);
        for (let i = 0; i < nodes.length; i++) {
          currentPath = Path.sep + nodes.slice(1, i + 1).join(Path.sep)
          lastPath = stack[stack.length - 1]
          parent = map[lastPath]
          if (!map[currentPath]) {
            node = {
              name: currentPath,
              value: getFileSizeSync(currentPath),
              children: []
            }
            parent.children.push(node);
            map[currentPath] = node;
          }
          stack.push(currentPath)
        }
        stack = stack.slice(0, 1)
      }
      calcSize (map[""])
      return map[""].children[0];
    }
    
    
    var tree = buildTree(["/path/to/file1", "/path/to/file2"])
    
    console.log (tree)
    

    【讨论】:

    • 我认为你错过了父目录应该sum文件大小的值
    • @Jamiec 确实!修复它
    • @SystemsRebooter 我也添加了那部分
    • ty ty C5H8NNaO4 :)
    猜你喜欢
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 2021-11-27
    • 2021-02-07
    相关资源
    最近更新 更多