The filter() method creates a new array with all elements that pass the test implemented by the provided callback function. It does not change the original array.
There are 2 variations for the syntax of Array.filter() method. They are:
filter<S>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackfn | required | A function that tests each element of the array. Return true to keep the element, false otherwise. It accepts three arguments: value, index, and array. |
thisArg | optional | Value to use as this when executing callbackfn. |
This method creates a new array with elements that pass the test implemented by the provided callback function. If the callback returns true, the element is included in the new array.
Returns value of type Array
.
filter(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[]
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackfn | required | A function that tests each element of the array. Return true to keep the element, false otherwise. It accepts three arguments: value, index, and array. |
thisArg | optional | Value to use as this when executing callbackfn. |
This method creates a new array with elements that pass the test implemented by the provided callback function. If the callback returns true, the element is included in the new array.
Returns value of type Array
.
In TypeScript, you can use the filter()
method to filter out even numbers from an array.
For example,
arr
with some initial values.filter()
method with a callback function that checks if a number is even.evenNumbers
.evenNumbers
to the console using the console.log()
method.const arr = [1, 2, 3, 4, 5, 6];
const evenNumbers = arr.filter(num => num % 2 === 0);
console.log(evenNumbers);
[2, 4, 6]
In TypeScript, you can use the filter()
method to filter out words with more than three letters from an array.
For example,
words
with some initial values.filter()
method with a callback function that checks if the length of a word is greater than three.longWords
.longWords
to the console using the console.log()
method.const words = ['cat', 'dog', 'elephant', 'rat'];
const longWords = words.filter(word => word.length > 3);
console.log(longWords);
['elephant']
In TypeScript, you can use the filter()
method to filter out objects from an array based on a specific property.
For example,
people
with some initial objects.filter()
method with a callback function that checks if the age property of each object is greater than 18.adults
.adults
to the console using the console.log()
method.const people = [
{ name: 'John', age: 17 },
{ name: 'Jane', age: 20 },
{ name: 'Jim', age: 16 },
{ name: 'Jake', age: 22 }
];
const adults = people.filter(person => person.age > 18);
console.log(adults);
[{ name: 'Jane', age: 20 }, { name: 'Jake', age: 22 }]
In this TypeScript tutorial, we learned about filter() method of Array: the syntax and few working examples with output and detailed explanation for each example.