javascript - AngularJS: how to find objects from an array of objects, given an array of property values in an array, using $filter -
have set of objects in array (items array) have property
item_id: [{item_id:1,...},{item_id:2,...}...]
have array set of
item_ids: [2, 8, 10]
how use $filter of angularjs array of objects items array item_id
matches in item_ids
array.
you can use custom filter kind of filtration. , can use use $filter
service if want in code instead of template.
see below code.
var app = angular.module('app', []); app.controller('ctrl', function($scope, $filter) { $scope.items = [{ item_id: 1 }, { item_id: 2 }, { item_id: 3 }, { item_id: 4 }]; $scope.findlist = [2, 4]; $scope.findlist2 = [3]; // using $filter service. $scope.usingservice = $filter('findobj')($scope.items, [1, 3]) }); app.filter('findobj', function() { return function(list, obj) { return list.filter(function(l) { if (obj.indexof(l.item_id) >= 0) { return true; } }); }; })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <div ng-controller="ctrl"> <div ng-repeat="item in items | findobj: findlist"> {{item}} </div> <hr/> <div ng-repeat="item in items | findobj: findlist2"> {{item}} </div> <hr/>{{usingservice}} </div> </div>
Comments
Post a Comment