如果键与underscore

如果键与underscore

本文介绍了如果键与underscore.js匹配,则比较两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有Facebook权限的数组以及用户应该给出的权限数组:

I have an array with permissions from Facebook and an array of the permissions that the user shouldve given:

window.FB.api('/me/permissions', function(perm){
    if(perm){
       var given_permissions =  _.keys(perm['data'][0];
       var needed_permissions = ["publish_stream", "email"];
       //now check if given permissions contains needed permissions
    }
}

现在我要比较所有 needed_permissions 是否在 given_permissions ,以下划线的方式(没有自己循环两个数组并比较值)。我看到了 _ .include 方法,但这会将数组与值进行比较。如果所有权限都可用,我想返回true,否则返回false。如果可能,我正在寻找一个不错的单行调用。

Now I want to compare if all the needed_permissions are in given_permissions, in an underscore savvy way (without looping two arrays myself and compare values). I saw a _.include method, but this compares an array with a value. I want to return true if all the permissions are available and else a false. I was looking for a nice single line call if possible.

原因是, FB.login 即使用户选择可以,也会返回true cel扩展权限。所以我需要再次检查这个。

The reason for this is, that FB.login returns true even if the user chooses to cancel the extended permissions. So I need to doublecheck this.

推荐答案

这个怎么样?

_.all(needed_permissions, function(v){
    return _.include(given_permissions, v);
});

这篇关于如果键与underscore.js匹配,则比较两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:29