将文件/目录结构转换为 vue json 中的“树”,如何将新字段添加到新的 json 类型?

詹姆士

将文件/目录结构转换为 vue json 中的“树”

我有一个看起来像这样的对象数组:

[
    {
        "name": "Officer",
        "fid": "123",
        "isDirectory": true,
        "path": "Officer/EventReport/SelfReport/110-04-02/RCBS.pdf"
    },
    {
        "name": "Officer",
        "fid": "456",
        "isDirectory": true,
        "path": "Officer/EventReport/SelfReport/110-04-10/1100_b.pdf"
    },
    {
        "name": "Officer",
        "fid": "875",
        "isDirectory": true,
        "path": "Officer/S_Meeting/W_Meeting/110-5/Officer_from.docx"
    },
    {
        "name": "Officer",
        "fid": "851",
        "isDirectory": true,
        "path": "Officer/S_Meeting/W_Meeting/110-5/16200.jpg"
    },
    {
        "name": "Officer",
        "fid": "510",
        "isDirectory": true,
        "path": "Officer/S_Meeting/W_Meeting/110-5/2021-05-18_2342.png"
    }
]

可能有任意数量的任意路径,这是遍历目录中的文件和文件夹的结果。

我想要做的是确定这些的“根”节点。最终,这将存储在 mongodb 中并使用物化路径来确定它的关系。

我希望我能展示这个。

[
    {
        "name": "Officer",   
        "isDirectory": true,
        "items": [
            {
                "name": "EventReport",  
                "isDirectory": true,
                "items": [
                    {
                        "name": "SelfReport",  
                        "isDirectory": true,
                        "items": [
                            {
                                "name": "2020-110-04-02",  
                                "isDirectory": true,
                                "items": [
                                    {
                                        "name": "RCBS.pdf",  
                                        "fid": "123",
                                        "isDirectory": false
                                    }
                                ]
                            },
                            {
                                "name": "2020-110-04-10", 
                                "isDirectory": true,
                                "items": [
                                    {
                                        "name": "1100_b.pdf", 
                                        "fid": "456",
                                        "isDirectory": false
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "name": "SecurityMeeting",  
                "isDirectory": true,
                "items": [
                    {
                        "name": "SecurityWorkMeeting", 
                        "isDirectory": true,
                        "items": [
                            {
                                "name": "2021-05-SecurityWorkMeeting", 
                                "isDirectory": true,
                                "items": [
                                    {
                                        "name": "Officer_Report.docx", 
                                        "fid": "875",
                                        "isDirectory": false
                                    },
                                    {
                                        "name": "16200.jpg", 
                                        "fid": "851",
                                        "isDirectory": false
                                    },
                                    {
                                        "name": "2021-05-18_2342.png",
                                        "fid": "510",
                                        "isDirectory": false
                                    }
                                ]
                            },      
                        ]
                    }
                ]
            }
        ]
    },    
]

这是我的代码,但我不知道如何添加 fid 字段,我尝试了更多方法,但都无法正常工作。

let arr = xhr.data.result[0].UploadFile;
            var tree = {};
            arr.forEach((item) => {
              //debugger;
              let tokens = item.path.replace(/^\/|\/$/g, "").split("/");
              let current = tree;
              for (let i = 0; i < tokens.length; i++) {
                if (!current[tokens[i]]) {
                  current[tokens[i]] = {};
                }
                current = current[tokens[i]];
              }
            });
            const parseNode = function (node) {
              return Object.keys(node).map((key) => {
                //debugger;
                if (Object.keys(node[key]).length === 0) {
                  return {
                    isDirectory: false,
                    name: key,
                  };
                }
                return {
                  isDirectory: true,
                  name: key,
                  items: parseNode(node[key]),
                };
              });
            };
objectProvider._data[0].items = parseNode(tree);

luvtas.com

像这样修改

let arr = xhr.data.result[0].UploadFile;
            var tree = {};
            arr.forEach((item) => {
              //debugger;
              let tokens = item.path.replace(/^\/|\/$/g, "").split("/");
              let current = tree;
              for (let i = 0; i < tokens.length; i++) {
                if (!current[tokens[i]]) {
                  current[tokens[i]] = {fid: item.fid};
                }
                current = current[tokens[i]];
              }
            });
            const parseNode = function (node) {
              return Object.keys(node).map((key) => {
                const { fid } = node[key];
                delete node[key].fid;
                if (Object.keys(node[key]).length === 0) {
                  return {
                    isDirectory: false,
                    name: key,
                    fid,
                  };
                }
                return {
                  isDirectory: true,
                  name: key,
                  fid,
                  items: parseNode(node[key]),
                };
              });
            };
objectProvider._data[0].items = parseNode(tree);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将文件系统中的目录结构转换为 Json 对象

scala spark 将结构类型列转换为 json 数据

如何将JSON类型的字段嵌套到一堆新字段中?

使用Node.js将文件系统中的目录结构转换为JSON

如何将 JSON 转换为绝对类型类型?

我可以将类型强制转换为 json 文件吗?

如何将私有字段添加到json

如何将新属性添加到JSON数组?

将 JSON 结果转换为对象类型

将JSON转换为复杂类型

将 JSON 转换为强类型对象

如何将ArrayList <String>添加到JSON数组-牢记类型安全

将新字段添加到JSONB中的嵌套JSON数组

在 CsvHelper 中,如何将 List<string> 类型的字段转换为 Json?

Vue 中的分页 JSON 应用程序:如何将类添加到当前页面项?

如何将 json-ld 添加到 Vue 3?

如何将Postgresql JSON类型转换为Golang本机?

如何使用字典将新字典添加到现有的json文件中?

如何将_添加到json对象中的空格?

如何将json添加到列表中

如何将目录的所有json文件转换为python中的文本文件?

MongoDB和Express:类型错误:将循环结构转换为JSON

Amazon Cognito 确认密码失败(类型错误:将循环结构转换为 JSON)

未捕获的类型错误:在 Angular/依赖项更新后将循环结构转换为 JSON

如何快速将 JSON 转换为结构类型数据?

如何将 JSON 结构转换为字典或结构

[Vue警告]:渲染错误:“ TypeError:将圆形结构转换为JSON

将字段添加到JSON文件

将CSV转换为JSON树结构?