gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:24:53 30 4
gpt4 key购买 nike

我坚持使用 JSON 中的两个模拟数据表从平面数组创建树结构。该表应匹配两个唯一标识以确定它们之间的层次结构。

带有组数据库数组的 JSON 看起来像这样:

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

]
};

包含 Users 数组的 JSON 如下所示:

 {
"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]
}
]
};

Hierarchy 应该是这样的,需要写入 JSON

 [
{
"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});
}

最佳答案

您可以为各种类型的数据采用哈希表,以便在不迭代对象数组的情况下更快地访问。

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

然后您需要为根对象添加一个新属性,并将其添加到 groups.groups 属性,以便所有级别都具有相同的访问类型。

最后,首先迭代 groups.users,然后迭代 groups.groups 以获取所有对象,对于组,也获取 child 。

在给定的数据中,我评论了未使用/重复的数据。

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; }

关于javascript - 使用 Javascript 中的两个数据表从平面数组构建树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51028894/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com