Filter filter
Description
The filter
filter filters an array and returns a new array.
We can use this filter to filter out items for display.
The filter method takes a string, object, or function that it will run to select or reject array elements.
If the first parameter passed in is:
- string - accept all elements that match against the string. To negate append
!
to the string. - object - compare objects that have a property name that matches. If we want to match against all properties, we can use the $ as the key.
- function - run the function over each element of the array.
Example
To select all of the words that have the letter e in them
{{ ['XML', 'Java', 'Angular', 'Oracle', 'CSS', 'Javascript'] | filter:'e' }}
To filter on objects, we can use the object filter notation. The following line filters list of people objects with a list of their favorite language:
{{ [{
'name': 'A',
'City': 'San Francisco',
'favorite language': 'Java'
}, {
'name': 'B',
'City': 'San Francisco',
'favorite language': 'Javascript'
}] | filter:{'favorite language': 'Java'}
}}
To filter based on a function:
{{ ['Kava', 'Java', 'to', 'css'] | filter:isCapitalized }}
Note
We can also pass a second parameter into the filter method.
- true - It runs a strict comparison of the two using angular.equals(expected, actual).
- false - It looks for a case-insensitive substring match.