使用 _.every 测试多个属性

Test multiple properties using _.every

有人要求我使用 lodash 的 _.every:

更改一些代码
//for every item in collection, check if "someProp" is true, 
//but only if "someProp2" isn't "-1". If "someProp" is true for 
//every item in collection, return true.

$scope.areAllTrue = function() {
    for(var i=0; i<$scope.collection.length; i++){
        if($scope.collection[i].someProp2 === -1) {
            continue;
        }
        if(!$scope.collection[i].someProp) {
            return false;
        }
    }
    return true;
};

所以跟随 lodash example of:

_.every(users, 'active', false);

我们得到:

$scope.areAllTrue = function() {
    return _.every($scope.collection, 'someProp', true)
};

这处理 "For every item in the collection, check if someProp is true, if all are true, return true." 但是我可以在这里做 "continue" 检查吗?

编辑:我能否以某种方式将两个谓词与“_.every”一起使用?喜欢 if someProp1 === true || someProp2 === -1 ?

_.every()可以使用谓词函数:

_.every(users, function(user) {
    return user.someProp2 === -1 || user.someProp;
});

你也可以跳过lodash,使用Array.prototype.every:

users.every(function(user) {
    return user.someProp2 === -1 || user.someProp;
});