Thursday 15 August 2013

javascript - Copying specific objects(according to given condition) from array of objects into an another array in angularJS -



javascript - Copying specific objects(according to given condition) from array of objects into an another array in angularJS -

i want create new array of objects existing array of objects , such new array have have objects matches status defined in if statement. below code snippet attempting

requirement when key.status=='completed' status gets nail ,only object have status : 'completed' in orders array copied temp array .but because of using angular.copy whole re-create of objects getting copied in temp array . .

--------html-----

<body ng-app ="mymodule"> <div ng-controller="mycontroller"> <div ng-repeat="order in temp"> {{order.id}} -- {{order.status}} -- {{order.name}} </div> </div> </body>

-------xxxx------

-------js----------

<script> var mymodule = angular.module('mymodule', []); mymodule.controller('mycontroller', function($scope) { $scope.orders = [ {id: '101' , status : 'completed' , name: 'jacopo' } , {id: '102' , status : 'rejected' , name: 'dan' } , {id: '103' , status : 'created' , name: 'erick' } ] ; $scope.temp = [ ] ; angular.foreach($scope.orders , function(key,value){ if(key.status == 'completed') { angular.copy($scope.orders,$scope.temp) } } ); } ) ; </script>

------xxx--------

you can using array.filter:

$scope.temp = $scope.orders.filter(function(o){return o.key === 'completed'});

javascript angularjs angularjs-ng-repeat controllers

No comments:

Post a Comment