Marco V
Marco V

Reputation: 2623

Check if all returned arrays contains the values false

console.log(allStatuses);

This variable returns two arrays:

[true, false, false, false, false]

[true, false, false, true, false]

Sometimes more arrays will be returned with true/false values.

I would like to check if all the returned arrays contain false and if so, do something.

What would be the best way to get this done?

Here is the code responsible:

angular.forEach($scope.prefAccount, function(account){
     if (typeof account === 'object') {
          var allStatuses = [];  
          angular.forEach(account, function(alertStatus){
               return allStatuses.push(alertStatus.enabled);
          })
          console.log(allStatuses);
     }                    
});

Upvotes: 0

Views: 99

Answers (2)

Oleksandr T.
Oleksandr T.

Reputation: 77482

You can use .every and .concat (to create one-dimensional array from two-dimensional)

var allStatuses = [
  [true, false, false, false, false],
  [true, false, false, true, false]
]
allStatuses = [].concat.apply([], allStatuses);

var isFalse = allStatuses.every(function (el) {
  return el === false;
})

console.log(isFalse);

Upvotes: 6

user663031
user663031

Reputation:

The shortest, most semantic version would be:

allStatuses.every(a => !a.some(Boolean))

Or without arrow functions:

allStatuses.every(function(a) { return !a.some(Boolean); })

In English:

Every array in allStatuses should not have some (any) true values.

Upvotes: 3

Related Questions