[英]JavaScript - recursive looping through object
I'm trying to make a simple ACL just for practice. 我正在尝试为练习制作一个简单的ACL。
I want to loop through my object data and if my currentViewer has a parent I want to add his path (access in my object) to arrayAccess. 我想遍历我的对象数据,如果我的currentViewer有父,我想将他的路径(在我的对象中访问)添加到arrayAccess。
So if my viewer is user his accessArray should be ["", home]
, if he's admin it would be ["", home, user]
and if he's a guest it will be only [""]
. 因此,如果我的观众是用户,他的accessArray应该是
["", home]
,如果他是管理员,它将是["", home, user]
,如果他是客人,它将只是[""]
。
How to do it in a recursive way, so I don't have to create thousands of for loops? 如何以递归方式执行此操作,因此我不必创建数千个for循环?
I was trying calling a checkParent()
inside my checkParent()
, but it only makes my loop infinitive. 我试图在
checkParent()
调用checkParent()
,但它只会使我的循环不定式。 I'm sorry if it's a silly question, I'm very beginning in JS. 我很抱歉,如果这是一个愚蠢的问题,我在JS开始。
var currentViewer = "user"; var data = { users: [{ role: "guest", access: "" }, { role: "user", access: "home", parent: "guest" }, { role: "admin", access: "user", parent: "user" } ] }; var accessArray = []; function checkRole() { var users = data.users; for (var i = 0; i < users.length; i++) { if (currentViewer === users[i].role) { accessArray.push(users[i].access); console.log(accessArray); function checkParent() { if (users[i].parent) { for (var j = 0; j < users.length; j++) { if (users[i].parent === users[j].role) { accessArray.push(users[j].access); console.log(accessArray); } } } }; checkParent(); } }; }; checkRole();
There are a bunch of ways to do it. 有很多方法可以做到这一点。 Simple way is just make a hash for easy look up and reference the parents on a loop and push.
简单的方法就是使哈希容易查找并在循环中引用父项并推送。
var data = { users : [ { role:"guest" , access:"" }, { role:"user" , access:"home", parent: "guest" }, { role:"admin" , access:"user", parent: "user" } ] }; var roleHash = data.users.reduce(function(o, i) { o[i.role] = i; return o; }, {}); function getAccessRoles(roleKey) { var role = roleHash[roleKey]; var roles = [role.access]; while (role.parent) { role = roleHash[role.parent]; roles.unshift(role.access); } return roles; } console.log("admin", getAccessRoles("admin")); console.log("user", getAccessRoles("user")); console.log("guest", getAccessRoles("guest"));
You can try this: 你可以试试这个:
var data = {
users : [
{ role:"guest" , access:"" },
{ role:"user" , access:"home", parent: "guest" },
{ role:"admin" , access:"user", parent: "user" }
]
};
var currentUser = "admin";
var arr = [];//array that you need
var defineAccess = function(item){
if(item.parent){
var index = data.users.map(function(e) { return e.role; }).indexOf(item.parent);
defineAccess(data.users[index]); //recursive calling
arr.push(data.users[index].access);
}
}
data.users.forEach(function(item){
if(currentUser === item.role){
arr.push(item.access);
defineAccess(item);
}
})
console.log(arr); //final output array
I don't think "recursiveness" should be a goal on its own... Why not create a Map of roles and their properties and use the parent
property to retrieve a new item from this map, until you can't? 我不认为“递归”本身应该是一个目标......为什么不创建一个角色及其属性的Map,并使用
parent
属性从这个地图中检索一个新项目,直到你不能?
var data = { users: [{ role: "guest", access: "" }, { role: "user", access: "home", parent: "guest" }, { role: "admin", access: "user", parent: "user" } ] }; var roles = new Map(data.users.map(u => [u.role, u])); var getAccess = function(role) { var access = []; var current = roles.get(role); while (current) { access.push(current.access); current = roles.get(current.parent); } return access.reverse(); } console.log("guest", getAccess("guest")) console.log("user", getAccess("user")) console.log("admin", getAccess("admin"))
build an object model first,then the problem is simple. 首先构建一个对象模型,然后问题很简单。
var data = { users: [ { role: "guest", access: "" }, { role: "user", access: "home", parent: "guest" }, { role: "admin", access: "user", parent: "user" } ] }; var users = data.users.reduce(function (roles, user) { roles[user.role] = user; return roles; }, {}); var accessList = data.users.map(function (user) { var all = []; do { all.unshift(user.access); user = users[user.parent]; } while (user); return all; }); console.log(accessList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.