在Javascript中使用两个数据表从平面数组构建树
我一直坚持使用JSON中的两个模拟数据表从平面数组创建树结构。 该表应匹配两个唯一id,以确定它们之间的层次结构 带有组DB数组的JSON如下所示: 包含用户的JSON数组如下所示: 这是第一个数据表的外观,决定了组之间的层次结构: 第二个类似于此并确定哪个用户属于哪个组: 层次结构应该是这样的,需要写入JSON 编辑:我试图创建一个递归函数来查找相关的组。 它可以工作,但我不知道如何组合用户在Javascript中使用两个数据表从平面数组构建树,javascript,arrays,structure,Javascript,Arrays,Structure,我一直坚持使用JSON中的两个模拟数据表从平面数组创建树结构。 该表应匹配两个唯一id,以确定它们之间的层次结构 带有组DB数组的JSON如下所示: 包含用户的JSON数组如下所示: 这是第一个数据表的外观,决定了组之间的层次结构: 第二个类似于此并确定哪个用户属于哪个组: 层次结构应该是这样的,需要写入JSON 编辑:我试图创建一个递归函数来查找相关的组。 它可以工作,但我不知道如何组合用户 function checkChildren(group) { const allChildre
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});
}
您可以为各种类型的数据创建一个哈希表,以便在不迭代对象数组的情况下更快地进行访问 对于用户,您仍然需要一个具有新属性和重命名键的新对象 然后,需要为根对象创建一个新属性,并将其添加到groups.groups属性中,以使所有级别具有相同的访问类型 在结束时,先迭代groups.users,然后迭代groups.groups,以获取所有对象,对于这些组,还需要子对象 在给定的数据中,我对未使用/重复的数据进行了注释 函数getNodesnode{ 返回[ …hash.groups.users[node]| |[].mapid=>hash.user[id], …hash.groups.groups[node]| |[].mapid=>Object.assignhash.group[id],{children:getNodesid} ]; } var db={ 组:[ {groupName:ROOT,id:1}, {groupName:Family,id:9}, {组名:BestFriends!,id:10}, {组名:Cars,id:4}, {组名:funHouse,id:3} ], 用户:[ {用户名:StrongGoose,密码:sdff12fdsa,年龄:31,id:2}, {用户名:John,密码:sdjd34fffdsa,年龄:31岁,id:3}, {用户名:Mary,密码:sdfffdsa,年龄:31,id:4} ], 组组组:[ {1:[9,10]},//好的 {10:[3]},//秒 { 3: [4] } ], 组用户:[ //{11:[2]},//从未使用过 { 3: [3] }, { 4: [4] }, {10:[2]},//首先 //{3:[3]}//重复 ] }, 散列={ group:Object.assign…db.group.map{id,groupName:name,type='group'}=>{[id]:{type,id,name}, user:Object.assign…db.user.mapo=>{[o.id]:o}, 小组:{ 组:Object.assign…db.GroupsToGroups,{root:db.group.filter{groupName}=>groupName==='root'.map{id}=>id}, 用户:Object.assign…db.GroupsToUsers } }, 结果=getNodes'root'; console.logresult;
.作为控制台包装器{max height:100%!important;top:0;}还是只是一个简单对象?你试过什么?嗨,iv'e用iv'e试过的东西编辑了这篇文章。我需要JSON。出于某种原因,我得到的只是:[{type:'group',id:1,name:'ROOT',children:[[Object],[Object]]}]请用JSON.stringifyresult,null,4查看结果。
{
"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});
}