The Array.filter() is an inbuilt TypeScript function which is used to creates a new array with all elements that pass the test implemented by the provided function.
Syntax:
array.filter(callback[, thisObject])
Parameter: This methods accepts two parameter as mentioned and described below:
- callback : This parameter is the Function to test for each element.
- thisObject : This parameter is the Object to use as this when executing callback.
Return Value: This method returns created array.
Below examples illustrate the Array filter() method in TypeScript
Example 1:
JavaScript
<script>
function ispositive(element, index, array)
{
return element > 0;
}
var arr = [ 11, 89, -23, 7, 98 ];
var value = arr.filter(ispositive);
console.log( value );
</script>
|
Output:
[11,89,7,98]
Example 2:
JavaScript
<script>
function isodd(element, index, array)
{
return (element % 2 == 1);
}
var arr = [ 11, 89, 23, 7, 98 ];
var value = arr.filter(isodd);
console.log( value );
</script>
|
Output:
[11,89,23,7]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Jun, 2020
Like Article
Save Article