return true if all objects in array has value in property

Viewed 4403

I have an array of objects, something like this:

$scope.objectArray = [
  {Title: 'object1', Description: 'lorem', Value: 57},
  {Title: 'object2', Description: 'ipsum', Value: 32},
  {Title: 'object3', Description: 'dolor', Value: 135}
]

I would like to check, and return true, if all objects in this array has an value inside the property 'value'.

I think I could do it with an forEach loop, but is there a better way than this?

var isTrue = true;
angular.forEach(objectArray, function(o){
  if (!o.Value){
    isTrue = false; // change variable 'isTrue' to false if no value
  }
});
3 Answers
Related