Robin James Kerrison
Robin James Kerrison

Reputation: 1757

AngularJS Filter: Return array of objects not matching expected string

In AngularJS, the 'filter' filter can look through an array of objects and return the array of only those with a string property value that contains a string comparator.

The following will thus give a list of every member of the array of objects people with any property like surname: 'Smith' or occupation: 'Blacksmith' or address_1: '123 Smithfield Lane' etc.

<ul>
    <li ng-repeat="person in people | filter: 'Smith'">
        {{ person.name }}
    </li>
</ul>

What would be the best way to get an array of only the objects which don't contain the string 'Smith' using AngularJS?

Upvotes: 1

Views: 837

Answers (1)

hansmaad
hansmaad

Reputation: 18905

From https://docs.angularjs.org/api/ng/filter/filter

The predicate to be used for selecting items from array.

Can be one of:

string: The string is used for matching against the contents of the array. All strings or objects with string properties in array that match this string will be returned. This also applies to nested object properties. The predicate can be negated by prefixing the string with !

<li ng-repeat="person in people | filter: '!Smith'">
    {{ person.name }}
</li>

If you want to filter by name only use an object predicate

<li ng-repeat="person in people | filter: { name : '!Smith'}">
    {{ person.name }}
</li>

Upvotes: 2

Related Questions