Question: so here is the situation i currently am having trouble with.

I want to check if a user has permission to view a page, with one single function.

So I have an array with key/values where i store permissions in.

```
{"authorities":[{"role":"gebruikersbeheer"},{"role":"kijken"},{"role":"plannen"}]};

```

Stored in `service.currentUser`, which can be called by using `service.currentUser.authorities`.

I have a function:

```
hasPermission: function (permission) {
            var role = { "role" : permission };
            for(perm in service.currentUser.authorities)
            {
                var test = service.currentUser.authorities[perm];
                if(test === role)
                {
                    return (!!(service.currentUser) && true);
                }
            }
            return false;
        }

```

I created the `test` variable to debug its value. And the `permission` parameter has the value 'gebruikersbeheer'.

Now i want to compare the value of the `perm` key with the `role` and check if it is true.
Now i have searched along the internet to do so, and i only found solutions which are not viable for me nor implementable. 

When i start debugging my `perm` has an integer value. Why is it not the name of the key? (Which must be "role")
Also, when i use Watch on the `role` and `test` variables they are completely the same but still i cant compare them and get a `true`.

(Dont mind the return statement at this point.)
Also i cannot modify the array. It is returned by spring security this way.

Is there any (nicer) way to check if `authorities` contains `role`?
It might be duplicate, but i couldnt find anything for my specific situation.

Cheers.

Answer: 
`service.currentUser.authorities` is an array this is why you're getting an integer for `perm` in `for(perm in service.currentUser.authorities)`.

The other problem is that you can't compare all the properties in the object using `===` (including prototype properties), so you need to compare explicit the values for the properties... or create a custom function to compare your objects. 

You can try with:

```
     hasPermission: function (permission) {
        var role = { "role" : permission };
        for(perm in service.currentUser.authorities)
        {
            var test = service.currentUser.authorities[perm];
            if(test["role"] === role["role"])
            {
                return (!!(service.currentUser) && true);
            }
        }
        return false;
    }

```

Hope this helps,
Since you are just concerned with whether something exists or not, you can use the [Array.Some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) function. It still loops through the array, but will stop upon hitting the first instance that is true. Your method is basically doing the same thing already, but this way it a bit cleaner, since it uses the actual elements of the array (instead of an index).

```
hasPermission: function (permission) {
    var authArray = service.currentUser.authorities;

    var hasAuth = authArray.some(function(kvp) {
        return kvp.role == permission; //function to determine a match
    });
    return hasAuth; 
}

```