如何做递归树数组函数? [英] How to do recursive tree array function?
问题描述
我有一个无限深的文件夹树阵列.每个文件夹都有一个hasPermission".布尔值.我想要实现的是当父文件夹(可以位于树中的任何位置)hasPermission"变化,都是孩子hasPermission"也会改变——当然,祖父文件夹hasPermission"不会改变.
I have an infinitely deep folders tree-array. Each folder have a "hasPermission" boolean. What I want to achieve is when a parent folder's (that can be located anywhere in the tree) "hasPermission" changes, all it's children "hasPermission" will also change - of course, the grandparent folders "hasPermission" will not change.
例如,在下面的文件夹树中.如果 folderId: '2.1' 改变权限,folderId: '3.1' 也会改变;其他人将保持不变.
For example, in the folder tree below. If folderId: '2.1' changes permission, folderId: '3.1' will also change; the others will stay the same.
const example = [
{
folderId: '1',
hasPermission: false,
children: [
{
folderId: '2.1',
hasPermission: false,
children: [
{
folderId: '3.1',
hasPermission: false,
children: [],
},
],
},
{
folderId: '2.2',
hasPermission: false,
children: [],
},
],
},
];
这个函数改变了hasPermission"递归.
This function changes the "hasPermission" recursively.
const changeChildrenPermission = (
folders
) => {
return folders.map(
({ id, children, hasPermission }) => ({
id,
hasPermission: (hasPermission === false && true) || false,
children: changeChildrenPermission(children),
})
);
};
这是我到目前为止所拥有的,我正在尝试查找 selectedId 的树,如果它匹配,然后调用changeChildrenPermission"功能.
Here is what I have so far, I'm trying to look up the tree for the selectedId, if it matches, then call the "changeChildrenPermission" function.
const setPermissionChange = (
folders,
selectedId
) => {
const a = folders.map((folder) => {
if (folder.id === selectedId) {
return {
id: folder.folderId,
hasPermission: (hasPermission === false && true) || false,
children: changeChildrenPermission(folder.children),
};
});
};
不知道接下来要做什么.
Not sure what to do afterwards.
推荐答案
// visitFolders: Walks the folder tree recursively and calls visitFolder for every folder.
// - If visitFolder returns true for a folder, the walk stops immediately.
// - If you want to visit all folders, visitFolder should return false.
// (Returning false can be omitted, because the default return value of functions is undefined, which is falsy.)
const visitFolders = (folders, visitFolder) => {
for (let folder of folders) {
if (visitFolder(folder) || visitFolders(folder.children, visitFolder)) {
return true;
}
}
return false;
};
const findFolderById = (folders, id) => {
let folderWithId = null;
visitFolders(folders, folder => {
if (folder.id === id) {
folderWithId = folder;
return true;
}
return false;
});
return folderWithId;
};
const setFolderPermission = (folder, hasPermission) => {
visitFolders([folder], folder => {
folder.hasPermission = hasPermission;
return false;
});
};
const folders = [
{
id: "1",
hasPermission: true,
children: [
{
id: "1.1",
hasPermission: true,
children: []
},
{
id: "1.2",
hasPermission: true,
children: [
{
id: "1.2.1",
hasPermission: true,
children: []
},
{
id: "1.2.2",
hasPermission: true,
children: []
}
]
},
{
id: "1.3",
hasPermission: true,
children: []
},
{
id: "1.4",
hasPermission: true,
children: []
}
]
},
{
id: "2",
hasPermission: true,
children: [
{
id: "2.1",
hasPermission: true,
children: []
},
{
id: "2.2",
hasPermission: true,
children: []
}
]
}
];
const folder = findFolderById(folders, "1.2");
if (folder !== null) {
setFolderPermission(folder, false);
}
visitFolders(folders, folder => {
console.log(folder.id, folder.hasPermission);
return false;
});
这篇关于如何做递归树数组函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!