簡體   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