1

I have a teamDetails array, within which is a squad array, within which are player objects. Each player object has an injured property which contains the value "true" or "false".

I want to write a function that loops through the array returning only players whose injured property evaluates to true.

This is what I have so far (not working):

$scope.injuredPlayerSearch = function() {
  var injuredPlayers = [];

  $scope.teamDetails.squad.forEach(function(o) {
      if (o[injured] === true) {
        injuredPlayers.push(o)
      }
    });

  return injuredPlayers;
}

I can't see what's wrong with this. If anyone can, would appreciate some help.

Paulos3000
  • 3,355
  • 10
  • 35
  • 68

6 Answers6

1

You do not need to write any function. angular is there for you.

var injuredPlayers = $filter('filter')($scope.teamDetails.squad, {injured:true}, true);

Here $filter is angular filter. Do dependency inject to your controler or sevice where you are using.

For more about angular filter refer here

Note: 2nd true is for strict type checking. it is equivalent to injured===true

EDIT

For showing it to directly on view angular has much better solution.

{{teamDetails.squad | filter:{injured:true}:true}}

For use in view no need any dependency injection or controller.

Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84
  • Ok, that looks like what I'm after. So if I put this in my controller... `$scope.injuredPlayers = $filter('filter')($scope.teamDetails.squad, {injured:true}, true);`, and then call `{{injuredPlayers}}` in my view, would that work? – Paulos3000 May 27 '16 at 09:13
  • You can directly use it on view if you just need to show only in view. Updating the answer. – Partha Sarathi Ghosh May 27 '16 at 09:17
0

If the iteration is within an array of array this is the correct implementation:

$scope.injuredPlayerSearch = function() {
  var injuredPlayers = [];

  $scope.teamDetails.forEach(function(t){
    t.squad.forEach(function(o) {
      if (o[injured] === true) {
        injuredPlayers.push(o)
      }
    });
  });

  return injuredPlayers;
}
Sajal
  • 4,359
  • 1
  • 19
  • 39
0

You could use filter to return players who are injured:

$scope.injuredPlayerSearch = function() {
    return $scope.teamDetails.squad.filter(function(o) {
        return o[injured];
    });
}
Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
0

try this

   var injuredPlayers = [];
  angular.forEach($scope.teamDetails.squad,function(s){
     if (s.injured === true) {
      injuredPlayers.push(s)
     }
  })
 return injuredPlayers;
Hadi J
  • 16,989
  • 4
  • 36
  • 62
0

Use the javascript filter

  var players = [{ id : 0 , injured : true},
                { id : 1 , injured : false},
                { id : 2 , injured : false},
                { id : 3 , injured : true},
                { id : 4 , injured : true}];

  var injuredPlayers = players.filter(filterByInjured)

  function filterByInjured(player) {
      if ('injured' in player && typeof(player.injured) === 'boolean' && player.injured === true) {
          return true;
      }
  }

  console.log(injuredPlayers);
Chantal
  • 959
  • 2
  • 12
  • 24
0

You did everything correct just left something

$scope.injuredPlayerSearch = function() {
     var injuredPlayers = [];

     angular.forEach($scope.teamDetails.squad,function(o) {
          if (o[injured] === true) {
                 injuredPlayers.push(o)
          }
     });

     return injuredPlayers;
}