Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从具有绝对路径的对象数组构造类似JSON的文件系统_Javascript_Json - Fatal编程技术网

Javascript 如何从具有绝对路径的对象数组构造类似JSON的文件系统

Javascript 如何从具有绝对路径的对象数组构造类似JSON的文件系统,javascript,json,Javascript,Json,我正在尝试为文件系统创建一个JSON文件。我在中发现了一些有趣的东西,但我需要得到如下结果: {path: 'root', size: 24, isEmpty: false, isDir: true, lastModified: 156727373, children: [ {path: 'root/folder1', size: 24, isEmpty: false, isDir: true,

我正在尝试为文件系统创建一个JSON文件。我在中发现了一些有趣的东西,但我需要得到如下结果:

{path: 'root',
  size: 24,
  isEmpty: false,
  isDir: true,
  lastModified: 156727373,
  children: [
        {path: 'root/folder1',
         size: 24,
         isEmpty: false,
         isDir: true,
         lastModified: 156727373,
         children: [{
               size:10,
               name:'file1',
               isDir:true,
               isEmpty:true,
               lastModified:10123123123
        }, {
               size:10,
               name:"file2",
               isDir:true,
               isEmpty:true,
               lastModified:10123123123
        }, {
               size:10,
               name:'file3',
               isDir:true,
               isEmpty:true,
               lastModified:10123123123}]}
来自一个数据数组,该数组的键“path”的值为绝对路径。像这样:

data:[{
    "path":"/root/folder1",
    "children":[
         {
            "size":10,
            "name":"file1",
            "isDir":true,
            "isEmpty":true,
            "lastModified":10123123123
         },
         {
            "size":10,
            "name":"file2",
            "isDir":true,
            "isEmpty":false,
            "lastModified":10123123123
         },
         {
            "size":10,
            "name":"file3",
            "isDir":false,
            "isEmpty":true,
            "lastModified":10123123123
         }
      ]}, {
    "path":"/root",
    "children":[
         {
            "size":10,
            "name":"folder1",
            "isDir":true,
            "isEmpty":false,
            "lastModified":10123123123
         }
      ]}
]
详情如下: 我有一个数据库表,其中每一行代表一个目录:绝对路径和子目录及其信息。单击子目录将触发一个异步请求,请求该目录的子目录的数据,DB将创建一行该目录的数据。绝对路径将用于对JSON中的各个文件夹和整个文件系统树进行排序

我试过:

var output = {};
var current;

for(var a=0; a<data.length; a++) {
  var s = data[a].path.split('/');
  current = output;
  for(var i=0; i<s.length; i++) {
    if(s[i] != '') {
      if(current[s[i]] == null) current[s[i]] = {};
      current = current[s[i]];
    }
  }
}
var输出={};
无功电流;

对于(var a=0;a您可以获取给定的路径和每个子对象的名称并找到该对象。然后更新该对象

var data=[{path:/root/folder1],子项:[{size:10,name:“file1”,isDir:true,isEmpty:true,lastModified:10123123123},{size:10,name:“file2”,isDir:true,isEmpty:false,lastModified:10123123},{size:10,name:“file3”,isDir:false,isEmpty:true,lastModified:10123123},{path:“/root”,子项:[{size:10,name:“folder1”,isDir:true,isEmpty:false,lastModified:10123123}]},
result=data.reduce((r,{path,children})=>{
var temp=path.slice(1.split('/');
子类。forEach((o)=>{
Object.assign(
临时雇员
.concat(o.name)
.map((_,i,a)=>a.slice(0,i+1).join('/'))
.reduce((q,path,i)=>{
var temp=(q.children=q.children | |[]).find(p=>p.path==path);
if(!temp)q.children.push(temp={path});
返回温度;
},r),
o
);
});
返回r;
}儿童[0];
console.log(结果);

。作为控制台包装{max height:100%!important;top:0;}
请添加一些数据和预期结果。是否要写入
var s=data[a]。path.split('/)