简体   繁体   中英

Matching a variable with an object inside a nested array with JavaScript

I am trying to find a match inside this JSON array but I find it a bit complicated since it's a nested array of objects.

I'm not sure what I am doing entire wrong here:

The idea is that I have an array with a set of permissions and I want to return only the set of permissions that match the role:

var data = [{
    "visitor": {
        "static": ["page-one:visit", "home-page:visit", "login"]
    }
}, {
    "users": {
        "static": ["posts:list", "posts:create", "users:getSelf", "home-page:visit", "dashboard-page:visit"]
    }
}, {
    "admin": {
        "static": ["posts:list", "posts:create", "posts:edit", "posts:delete", "users:get", "users:getSelf", "home-page:visit", "dashboard-page:visit"]
    }
}]


var role = "admin"

for(var x=0;x <data.length;x++){

  if(role === data[x]){
    console.log("OLE, we got a match!" + data[x])
  }

}

For some reason I just can't find a match. I just wanna return the full object like:

"admin":{ 
        "static": ["posts:list", "posts:create", "posts:edit", "posts:delete", "users:get", "users:getSelf", "home-page:visit", "dashboard-page:visit"]
    }

Here is a JS Bin Link.

You could use the .find function like below:

data.find(function(x){ return Object.keys(x).indexOf(role) > -1; });

Given your role is the key of the object, you need to check if the object itself contains the role as a key, for this you'd use Object.keys(<object>).indexOf(role) where indexOf will return the value of -1 if it's not found and 0+ if found.

 var data = [{"visitor":{"static":["page-one:visit","home-page:visit","login"]}},{"users":{"static":["posts:list","posts:create","users:getSelf","home-page:visit","dashboard-page:visit"]}},{"admin":{"static":["posts:list","posts:create","posts:edit","posts:delete","users:get","users:getSelf","home-page:visit","dashboard-page:visit"]}}] var role = "admin" var admins = data.find(function(x){ return Object.keys(x).indexOf(role) > -1; }); console.log(admins);

if you wanted to accommodate for an array of different roles, you can use the following, easy to follow example.

 var data = [{"visitor":{"static":["page-one:visit","home-page:visit","login"]}},{"users":{"static":["posts:list","posts:create","users:getSelf","home-page:visit","dashboard-page:visit"]}},{"admin":{"static":["posts:list","posts:create","posts:edit","posts:delete","users:get","users:getSelf","home-page:visit","dashboard-page:visit"]}}] var role = ["admin", "visitor"]; var admins = role.map(function(role) { return getObjectsForRole(role); }) function getObjectsForRole(role) { return data.find(function(x){ return Object.keys(x).indexOf(role) > -1; }); } console.log(admins);

The above is pretty much the same as before, but we're mapping ( .map ) each role and calling a function which contains our call to the .find function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM