【问题标题】:Coffeescript: Check whether value in jsonCoffeescript:检查json中的值是否
【发布时间】:2014-11-04 04:42:45
【问题描述】:

检查json中的值是否可用。我正在使用以下代码

        $scope.bindRoles.push
            Name: "Name"
            Editable: if item in $scope.rolesOfLoggedUser then true else false // here i am checking if item is present in $scope.rolesOfLoggedUser then set value of Editable as true.

其中,$scope.rolesOfLoggedUser = ["Admin", "Role1", "Role2"]

我的要求是,我想在 if 语句中再检查一件事。 $scope.rolesOfLoggedUser 中存在 item 或“Admin”,然后将 Editable 的值设置为 true。

我该怎么做?

请帮忙, 谢谢

【问题讨论】:

  • 把最后多余的语句去掉即可:Editable: item in $scope.rolesOfLoggedUser

标签: json coffeescript


【解决方案1】:

类似的东西?

Editable: if ('Admin' in $scope.rolesOfLoggedUser or item in $scope.rolesOfLoggedUser) then yes else no

我不确定您所说的 .. 项目或“管理员”是什么意思。你是说如果 rolesOfLoggedUser 不为空 = 有一个项目或?这里的项目是什么?

那么也许:

Editable: if ('Admin' in $scope.rolesOfLoggedUser or $scope.rolesOfLoggedUser.length > 0) then yes else no

但是,如果您想知道是否只有一个项目,那么该数组中的任何值都不是“可编辑”的,因此您不必显式测试“Admin”,对吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 2016-04-13
    • 2012-06-23
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多