简体   繁体   English

将键/值数组中的对象与另一个对象(键/值)进行比较

[英]Comparing objects from key/value array with another object (Key/value)

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 . 存储在service.currentUser ,可以使用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. 我创建了test变量以调试其值。 And the permission parameter has the value 'gebruikersbeheer'. permission参数的值为'gebruikersbeheer'。

Now i want to compare the value of the perm key with the role and check if it is true. 现在,我想将perm键的值与role进行比较,并检查它是否为真。 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. 当我开始调试时,我的perm有一个整数值。 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 . (必须是“角色”)此外,当我在roletest变量上使用Watch时,它们是完全相同的,但仍然无法比较它们并得出true

(Dont mind the return statement at this point.) Also i cannot modify the array. (此时不要在意return语句。)我也无法修改数组。 It is returned by spring security this way. 这样,Spring Security会将其返回。

Is there any (nicer) way to check if authorities contains role ? 有什么(更简单的方法)检查authorities包含role It might be duplicate, but i couldnt find anything for my specific situation. 它可能是重复的,但我找不到适合自己特定情况的任何东西。

Cheers. 干杯。

You currently are check a object with another object, is best check the string with the string, below show an little example of the same function but using the some method from arrays; 您当前正在用另一个对象检查一个对象,最好是用该字符串检查该字符串,下面显示了相同功能的一个小例子,但使用了数组中的some方法;

 var currentUser = {"authorities":[{"role":"gebruikersbeheer"},{"role":"kijken"},{"role":"plannen"}]}; function hasPermission(permission) { return currentUser.authorities.some(function(v){ return v.role === permission; }); } alert("Have permission? "+ hasPermission("gebruikersbeheer")) 

service.currentUser.authorities is an array this is why you're getting an integer for perm in for(perm in service.currentUser.authorities) . service.currentUser.authorities是一个数组,这就是为什么要在for(perm in service.currentUser.authorities)获得perm的整数的原因。

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 function. 由于您只关心是否存在某些内容,因此可以使用Array.Some函数。 It still loops through the array, but will stop upon hitting the first instance that is true. 它仍然循环遍历数组,但是在遇到第一个为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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用另一个数组/对象javascript的键值替换数组/对象的键值 - Replace array/objects key value with key value from another array/object javascript 将对象数组中键的值与AngularJS中的变量进行比较 - Comparing the value of a key in an array of objects to a variable in AngularJS javascript用从另一个对象数组的键值获得的键创建对象 - javascript create an object with key obtained from value of a key from another array of objects 按包含在另一个对象中的键和值过滤对象数组 - Filter array of objects by key and value contained in another object 根据对象键值从对象数组中删除对象 - Remove object from array of objects based on object key value 从数组中的键向对象数组 object 添加键和值 - Add key and value to array of objects object from the keys in array 如何从具有值作为另一个对象数组的 object 中删除键值对? - How to remove key value pair from an object having values as another array of objects? 从对象数组中获取特定的值[{“ key”:“ value”},{“ key”:“ value”}] - Get specific value from array of objects [{“key”:“value”}, {“key”:“value”}] 使用 JavaScript 将键值添加到另一个数组中的所有对象 - Add key value to all objects in array from another with JavaScript 将多个对象作为值添加到另一个对象中 - Add multiple objects as value to key in another object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM