javascript - How to filter json based on a query dynamically generated? -
i want filter json object using js angular project. json structure this:
{ "docs": [ { "firstname": "", "lastname": "", "birthdate": "", "city": "", "country": "" }, { "firstname": "", "lastname": "", "birthdate": "", "city": "", "country": "" } ] }
the query builder similar angular-query-builder
the query like:
(firstname = tom or (firstname = jerry , lastname = jack))
based on query js function should filter json object , console resulting json .
you can use custom filter (if don't want use query builder) filter out values based on firstname , lastname
function myctrl($scope){ $scope.people = { "docs": [ { "firstname": "tom", "lastname": "larry", "birthdate": "", "city": "", "country": "" }, { "firstname": "jerry", "lastname": "jack", "birthdate": "", "city": "", "country": "" }, { "firstname": "tom", "lastname": "thomas", "birthdate": "", "city": "", "country": "" }, { "firstname": "jerry", "lastname": "gaer", "birthdate": "", "city": "", "country": "" } ] } $scope.myfilter = function(item){ if((item.firstname == "tom")||(item.firstname == "jerry"&&item.lastname == "jack")){ return true; } }; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app> <div ng-controller="myctrl"> <ul> <li ng-repeat="person in people.docs | filter:myfilter"> {{person.firstname}} {{person.lastname}} </li> </ul> </div> </div>
Comments
Post a Comment