繁体   English   中英

我可以从嵌套数组中删除冗余数据结构包装器到嵌套json脚本吗?

[英]Can I remove a redundant data structure wrapper from my nested array to nested json script?

我编写了这个脚本,将具有下面结构的嵌套数组转换为具有父子关系的嵌套对象。

list = [
    ['lvl-1 item-1', 'lvl-2 item-1'],
    ['lvl-1 item-1', 'lvl-2 item-1', 'lvl-3 item-1'],
    ['lvl-1 item-1', 'lvl-2 item-1', 'lvl-3 item-2'],
    ['lvl-1 item-2', 'lvl-2 item-1', 'lvl-3 item-1'],
    ['lvl-1 item-2', 'lvl-2 item-2', 'lvl-3 item-2', 'lvl-4 item-1'],
];

它似乎可以解决问题,但是为了data.children脚本,我必须在初始数据结构周围添加data.children包装器。 我不相信这是必要的,尽管我还没有能够锻炼如何摆脱它。

任何人都能看到我失踪的东西吗?

console.log(nestedArrayToJson(list));

function nestedArrayToJson(structure) {
    const top_item = '0';

    // This was added to behave like the child data structure.
    let data = {
        children: [
            {
                name: top_item,
                parent: null,
                children: [],
            }],
    };

    for(let i = 0; i < structure.length; i++) {
        let parents = [top_item];
        for(let j = 0; j < structure[i].length; j++) {
            let obj = data;
            for(parent of parents) {
                obj = obj.children.find(o => o.name === parent);
            }
            const name = structure[i][j];
            if(!obj.children.find(o => o.name === name)) {
                obj.children.push({
                    name,
                    parent,
                    children: [],
                });
            }
            parents.push(structure[i][j]);
        }
    }

    return data.children[0];
}

样本输出

{
  "name": "0",
  "parent": null,
  "children": [
    {
      "name": "lvl-1 item-1",
      "parent": "0",
      "children": [
        {
          "name": "lvl-2 item-1",
          "parent": "lvl-1 item-1",
          "children": [
            {
              "name": "lvl-3 item-1",
              "parent": "lvl-2 item-1",
              "children": []
            },
            {
              "name": "lvl-3 item-2",
              "parent": "lvl-2 item-1",
              "children": []
            }
          ]
        }
      ]
    },
    {
      "name": "lvl-1 item-2",
      "parent": "0",
      "children": [
        {
          "name": "lvl-2 item-1",
          "parent": "lvl-1 item-2",
          "children": [
            {
              "name": "lvl-3 item-1",
              "parent": "lvl-2 item-1",
              "children": []
            }
          ]
        },
        {
          "name": "lvl-2 item-2",
          "parent": "lvl-1 item-2",
          "children": [
            {
              "name": "lvl-3 item-2",
              "parent": "lvl-2 item-2",
              "children": [
                {
                  "name": "lvl-4 item-1",
                  "parent": "lvl-3 item-2",
                  "children": []
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

可以通过向命名函数提取一些功能来清除for循环。

const node = (name, parent = null) => ({name, parent, children: []})处理创建节点。

然后可以使用addNode()添加节点

搜索当前下一个父节点findNamedNode()

如果找到具有current名称的node ,则它将向下移动到下一个node 如果不存在具有current名称的node ,则创建该node

function createTree(arr, topItem = 'Top') {
    const node = (name, parent = null) => ({name, parent, children: []});
    const addNode = (parent, child) => {
        parent.children.push(child);

        return child;
    };
    const findNamedNode = (name, parent) => {
        for(const child of parent.children) {
            if(child.name === name) { return child; }
            const found = findNamedNode(name, child);
            if(found) { return found; }
        }
    };

    const top = node(topItem);
    let current;

    for(const children of arr) {
        current = top;
        for(const name of children) {
            const found = findNamedNode(name, current);
            current = found ? found : addNode(current,
                node(name, current.name));
        }
    }

    return top;
}

感谢@ Blindman67对Code Review的帮助。

https://codereview.stackexchange.com/questions/219418/convert-nested-array-of-values-to-a-tree-structure/

暂无
暂无

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

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