Return objects in array with 'true' parameters
Return objects in array with 'true' parameters
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.
Answer by Dev-One for Return objects in array with 'true' parameters
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; }
Answer by Moishe Lipsker for Return objects in array with 'true' parameters
You could use filter
to return players who are injured:
$scope.injuredPlayerSearch = function() { return $scope.teamDetails.squad.filter(function(o) { return o[injured]; }); }
Answer by SSH for Return objects in array with 'true' parameters
try this
var injuredPlayers = []; angular.forEach($scope.teamDetails.squad,function(s){ if (s.injured === true) { injuredPlayers.push(s) } }) return injuredPlayers;
Answer by steur36 for Return objects in array with 'true' parameters
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);
Answer by Partha Sarathi Ghosh for Return objects in array with 'true' parameters
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.
Answer by Abhishek Panda for Return objects in array with 'true' parameters
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; }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment