代码之家  ›  专栏  ›  技术社区  ›  Anatsu

使用JavaScript中的两个数据表从平面数组构建树

  •  1
  • Anatsu  · 技术社区  · 6 年前

    我一直在用JSON中的两个模拟数据表从平面数组创建树结构。 表应与两个唯一ID匹配,以确定它们之间的层次结构。

    带有groups db array的json如下所示:

     {
    "group": [
        {
            "groupName": "ROOT",
            "id": 1
        },
        {
            "groupName": "Family",
            "id": 9
        },
        {
            "groupName": "BestFriends!",
            "id": 10
        },
         {
            "groupName": "Cars",
            "id": 4
        },
          {
            "groupName": "funHouse",
            "id": 3
        }
    
    ]
     };
    

     {
    "user": [
        {
            "username": "StrongGoose",
            "password": "sdff12fdsa",
            "age": 31,
            "id": 2
        },
        {
            "username": "John",
            "password": "sdjd34fffdsa",
            "age": 31,
            "id": 3
        },
        {
            "username": "Mary",
            "password": "sdfffdsa",
            "age": 31,
            "id": 4
        }
    ]
     };
    

     {
    "GroupsToGroups": [
        {
            "1":[9,10]
        },
        {
            "10":[3]
        }
    
    ]
     };
    

     {
    "GroupsToUsers": [
        {
            "11":[2]
        },
        {
            "3":[3]
        },
        {
            "4":[4]
        },
        {
        "10":[2] 
        },
        {
        "3":[3] 
        }
       ]
      };
    

     [
    {
        "type": "group",
        "id": "1",
        "name": "ROOT",
        "items": [
            {
                "type": "group",
                "id": "9",
                "name": "Family",
                "items": []
            },
            {
                "type": "group",
                "id": "10",
                "name": "BestFriends!",
                "items": [
                    {
                        "username": "StrongGoose",
                        "password": "sdff12fdsa",
                        "age": 31,
                        "id": 2
                    },
    
                    {
                        "type": "group",
                        "id": "3",
                        "name": "funHouse",
                        "items": [
                            {
                                "username": "John",
                                "password": "sdjd34fffdsa",
                                "age": 31,
                                "id": 3
                            },
                            {
                                "type": "group",
                                "id": "4",
                                "name": "Cars",
                                "items": [
                                    {
                                        "username": "Mary",
                                        "password": "sdfffdsa",
                                        "age": 31,
                                        "id": 4
                                    }
                                ],
                            }
                        ]
                    }
                ]
            }
    
        ]
      }
    
    
     ];
    

    编辑

     function checkChildren(group) {
      const allChildren = insideGroups[group.id];
      if (!allChildren) return group;
      const childGroups = allChildren.map((findChildrenID) => {
          const indexGroups = groups.findIndex((subGroup) => subGroup.id === 
        findChildrenID);
        return checkChildren(groups[indexGroups]);
        });
       return Object.assign({}, group, {groups: childGroups});
       }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Nina Scholz    6 年前

    您可以为各种类型的数据取一个哈希表,以便更快地访问,而无需迭代对象数组。

    对于用户,您无论如何都需要一个具有新属性和重命名键的新对象。

    然后需要根对象的新属性并将其添加到 groups.groups 属性对所有级别具有相同的访问类型。

    首先迭代结束 groups.users 然后 组.组 要获得所有对象和组,也要带上孩子。

    在给定的数据中,我对未使用/重复的数据进行了注释。

    function getNodes(node) {
        return [
            ...(hash.groups.users[node] || []).map(id => hash.user[id]),
            ...(hash.groups.groups[node] || []).map(id => Object.assign(hash.group[id], { children: getNodes(id) }))
        ];
    }
    
    var db = {
            group: [
                { groupName: "ROOT", id: 1 },
                { groupName: "Family", id: 9 },
                { groupName: "BestFriends!", id: 10 },
                { groupName: "Cars", id: 4 },
                { groupName: "funHouse", id: 3 }
            ],
            user: [
                { username: "StrongGoose", password: "sdff12fdsa", age: 31, id: 2 },
                { username: "John", password: "sdjd34fffdsa", age: 31, id: 3 },
                { username: "Mary", password: "sdfffdsa", age: 31, id: 4 }
            ],
            GroupsToGroups: [
                { 1: [9, 10] }, // ok
                { 10: [3] },    // second
                { 3: [4] }
            ],
            GroupsToUsers: [
                //{ 11: [2] }, // never used
                { 3: [3] },
                { 4: [4] },
                { 10: [2] },   // first
                //{ 3: [3] }   // dupe
            ]
        },
        hash = {
            group: Object.assign(...db.group.map(({ id, groupName: name, type = 'group' }) => ({ [id]: { type, id, name } }))),
            user: Object.assign(...db.user.map(o => ({ [o.id]: o }))),
            groups: {
                groups: Object.assign(...db.GroupsToGroups, { root: db.group.filter(({ groupName }) => groupName === 'ROOT').map(({ id }) => id) }),
                users: Object.assign(...db.GroupsToUsers)
            }
        },
        result = getNodes('root');
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }