繁体   English   中英

热总结 JavaScript 中文件夹结构中的文件?

[英]Hot to sum up files in folder structure in JavaScript?

我有给定的数组:

Array (7812)
0 {foldername: "", amount_of_files: 12, children: 86}
1 {foldername: "/pm", amount_of_files: 3, children: 7}
2 {foldername: "/pm/css", amount_of_files: 1, children: 0}
3 {foldername: "/pm/js", amount_of_files: 1, children: 0}
4 {foldername: "/stohg", amount_of_files: 1, children: 2}
5 {foldername: "/stohg/de", amount_of_files: 6, children: 1}
6 {foldername: "/stohg/de/pictures", amount_of_files: 21, children: 1}
...

我想将所有文件总结到第一级:

1 {foldername: "/pm", all_files: 5}
2 {foldername: "/stohg", all_files: 28}
...

我认为部分解决方案是:

array.filter(e => {
  const folders = e.foldername.split("/");
  if (folders.length === 1) {
    //maybe something with folders.reduce(); ?
  }
})

你可以有一个简单的正则表达式,并且可以在一个 reduce 方法中做到这一点。 在这里,我们正在检查模式/<word>

 var regEx = new RegExp(/^\/\w+$/, 'gm'); var folders = [{foldername: "", amount_of_files: 12, children: 86}, {foldername: "/pm", amount_of_files: 3, children: 7}, {foldername: "/pm/css", amount_of_files: 1, children: 0}, {foldername: "/pm/js", amount_of_files: 1, children: 0}, {foldername: "/stohg", amount_of_files: 1, children: 2}, {foldername: "/stohg/de", amount_of_files: 6, children: 1}, {foldername: "/stohg/de/pictures", amount_of_files: 21, children: 1}]; var sum = folders.reduce((acc, folder) => { if (regEx.test(folder.foldername)) { acc += folder.amount_of_files; } return acc; }, 0); console.log(sum);

在 summing.amount_of_files up Output 之前用斜线分割并将它们组合在一起,格式与您在帖子中提到的略有不同,但结果是相同的

<script>
function get_root_folder_path(path) {
    if( path === "" )
    return "/"; 
    var names = path.split("/");
  console.log(names);
    if( names.length >= 1 ) return "/" + names[1]; //note: names[0] === ""
  else return "INVALI3D";
}

var objs = [
{foldername: "", amount_of_files: 12, children: 86},
{foldername: "/pm", amount_of_files: 3, children: 7},
{foldername: "/pm/css", amount_of_files: 1, children: 0},
{foldername: "/pm/js", amount_of_files: 1, children: 0},
{foldername: "/stohg", amount_of_files: 1, children: 2},
{foldername: "/stohg/de", amount_of_files: 6, children: 1},
{foldername: "/stohg/de/pictures", amount_of_files: 21, children: 1},
];


// create an object to store your new root folder data
var root_folders = {};

for(var i = 0;i < objs.length;i ++) {
    var root_folder_path = get_root_folder_path(objs[i].foldername);
  if( root_folders[root_folder_path] === undefined )
    root_folders[root_folder_path] = {amount_of_files: objs[i].amount_of_files};
  else
    root_folders[root_folder_path].amount_of_files += objs[i].amount_of_files;
}

console.log(root_folders);

</script>

输出:

{
  /: {
    amount_of_files: 12
  },
  /pm: {
    amount_of_files: 5
  },
  /stohg: {
    amount_of_files: 28
  }
}

jsfiddle: https://jsfiddle.net/3f7kgLvq/

我自己解决了,我想提供解决方案来帮助找到我帖子的其他人。

array.forEach(e => {
      const folders = e.foldername.split("/");
      const isRoot = folders.length === 2;
      if (isRoot) {
        result.push(e);
      } else {
        result.forEach((elem, index) => {
          if (elem.foldername === "/" + folders[1]) {
            result[index] = {...result[index], amount_of_files: elem.amount_of_files + e.amount_of_files}
          }
        })
      }
    });
    console.log(result);

暂无
暂无

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

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