0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Does the solution work if I have the variable role set to an array? Something like this var role = ["admin", "visitor"] ? Would I return both sets of permissions then ?
No. That wasn't your question why would I think outside the box to accommodate made up requirements? :)
I was just asking generally. Thank you for providing a solution and an explanation tho'. I was just being curious how it could work with two roles..
@someonewithakeyboard It's ok to ask questions, but put them in your main question rather than a comment. See my edit :)
Actually now that I think of it, it doesn't even make sense to have several results. Disregard that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.