0

Can anybody tell how to search for a string in all properties. If match in some property I need to push the object in array in AngularJS.

I have an array

$scope.ListOfPeople = [
    { PersonID: 10, FirstName: "John", LastName: "Smith", Sex: "Male" },
    { PersonID: 11, FirstName: "James", LastName: "Last", Sex: "Male" },
    { PersonID: 12, FirstName: "Mary", LastName: "Heart", Sex: "Female" },
    { PersonID: 13, FirstName: "Sandra", LastName: "Goldsmith", Sex: "Female" },
    { PersonID: 14, FirstName: "Shaun", LastName: "Sheep", Sex: "Male" },
    { PersonID: 15, FirstName: "Nicola", LastName: "Smith", Sex: "Male" }
];

If the user types some value in search textbox. I need to search with PersonID, FirstName, LastName, Sex with all properties if it is match need to push the matched object.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
user386430
  • 4,837
  • 13
  • 41
  • 45
  • Possible duplicate of [How to search JSON tree with jQuery](http://stackoverflow.com/questions/5288833/how-to-search-json-tree-with-jquery) – Dan Wilson Nov 14 '16 at 14:54
  • What have you tried so far ? There's lot of documentation on internet about the search filter – Alexis Nov 14 '16 at 14:54
  • http://stackoverflow.com/a/10679626/5115768 you tried this? – lenny Nov 14 '16 at 14:55
  • Possible duplicate of [Search a javascript object for a property with a specific value?](http://stackoverflow.com/questions/9422756/search-a-javascript-object-for-a-property-with-a-specific-value) – Heretic Monkey Nov 14 '16 at 15:01

2 Answers2

2

I think Angular's Filter filter will do what you are looking for

<input ng-model="search"/>
<div ng-repeat="person in ListOfPeople | filter:search">
    {{person}}
</div>
rob
  • 17,995
  • 12
  • 69
  • 94
0

Something like that ?

for(var index in $scope.ListOfPeople) { 
   if ($scope.ListOfPeople.hasOwnProperty(index)) {
       if($scope.ListOfPeople[index] == "Your string") {
           // Do something here 
       }
   }
}