user386430
user386430

Reputation: 4967

How to search a string in all properties of an object in AngularJS

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.

Upvotes: 0

Views: 722

Answers (2)

user4676340
user4676340

Reputation:

Something like that ?

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

Upvotes: 0

rob
rob

Reputation: 18513

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>

Upvotes: 2

Related Questions