简体   繁体   English

从对象构建目录树

[英]Build a directory tree from an object

Having an array of items with the following format: 具有以下格式的项目数组:

[ { path: '/Folder 1/Folder 1.1',
    name: 'Folder 1.1.1',
    id: 'Fi6CsP4RWFutOZKsIDoYMSfBPQb-A-lj3C4Jc_zZoG0' },
  { path: '/Folder 1',
    name: 'Folder 1.2',
    id: 'c2dTN3CgBr9Xik8jdpBkfzR6wZ00oGTX3IbfXrfFujM' },
  { path: '/Folder 1',
    name: 'Folder 1.1',
    id: 'WmKaOZhzpubcunNNoxbUnqfSYVuNQZDNC852KDJK_G8' },
  { path: '/',
    name: 'Folder 1',
    id: 'aNRvIvCLyNOgLOmZVzFhoOiZMAz3-p87kBFIGSQS2Yg' },
  { path: '/',
    name: 'Folder 2',
    id: 'S4FkkQ3hgLTVedIrlSBqe2_1DhrrnLx5szk7-9Wv3X8' } ]

It is possible to take it to a format like the following 可以采用以下格式

var dir = {
"directory": [{
        "text": "Folder 1",
        "id": 'aNRvIvCLyNOgLOmZVzFhoOiZMAz3-p87kBFIGSQS2Yg',
        "nodes": [{
            "text": "Folder 1.1",
            "id": 'WmKaOZhzpubcunNNoxbUnqfSYVuNQZDNC852KDJK_G8',
            "nodes": [{
                "text": "Folder 1.1.1",
                "id": 'Fi6CsP4RWFutOZKsIDoYMSfBPQb-A-lj3C4Jc_zZoG0'
            }]
            }, {
            "text": "Folder 1.2",
            "id": 'c2dTN3CgBr9Xik8jdpBkfzR6wZ00oGTX3IbfXrfFujM'
        }]
    },
    {
        "text": "Folder 2",
        "id": 'S4FkkQ3hgLTVedIrlSBqe2_1DhrrnLx5szk7-9Wv3X8'
    }
]
};

Graphically Tree 图形

I was thinking using recursion, but I still have not been able to do it. 我当时在考虑使用递归,但我仍然无法做到这一点。

Here's a non-recursive solution that works by first building a tree, then transforming the tree into the desired structure. 这是一种非递归解决方案,其工作原理是先构建一棵树,然后将树转换为所需的结构。

The reason for the first step is due to the slowness of iterating over node arrays in linear time and assumes no order of the flat input array. 第一步的原因是由于在线性时间内迭代node数组的速度较慢,并且不考虑平面输入数组的顺序。 Performing lookups on an object tree structure speeds up and simplifies the process, making the result tree easy to build in a single traversal. 在对象树结构上执行查找可以加快并简化过程,从而使结果树易于在单个遍历中构建。

 const treeify = data => data.reduce((a, e) => { let level = a; e.path.split("/") .filter(e => e) .forEach(dir => { if (!(dir in level)) { level[dir] = {nodes: {}}; } level = level[dir].nodes; }) ; if (e.name in level) { level[e.name] = { text: e.name, id: e.id, nodes: level[e.name].nodes }; } else { level[e.name] = {text: e.name, id: e.id}; } return a; }, {}) ; const format = tree => { const result = []; const stack = [[result, tree]]; while (stack.length) { const [res, curr] = stack.pop(); for (const k in curr) { const o = { id: curr[k].id, text: curr[k].text }; res.push(o); if (curr[k].nodes) { o.nodes = []; stack.push([o.nodes, curr[k].nodes]); } } } return {directory: result}; }; const data = [ { path: '/Folder 1/Folder 1.1', name: 'Folder 1.1.1', id: 'Fi6CsP4RWFutOZKsIDoYMSfBPQb-A-lj3C4Jc_zZoG0' }, { path: '/Folder 1', name: 'Folder 1.2', id: 'c2dTN3CgBr9Xik8jdpBkfzR6wZ00oGTX3IbfXrfFujM' }, { path: '/Folder 1', name: 'Folder 1.1', id: 'WmKaOZhzpubcunNNoxbUnqfSYVuNQZDNC852KDJK_G8' }, { path: '/', name: 'Folder 1', id: 'aNRvIvCLyNOgLOmZVzFhoOiZMAz3-p87kBFIGSQS2Yg' }, { path: '/', name: 'Folder 2', id: 'S4FkkQ3hgLTVedIrlSBqe2_1DhrrnLx5szk7-9Wv3X8' } ]; console.log(format(treeify(data))); 

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

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