TypeScript | Array filter() Method

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // check for positive number 
    function ispositive(element, index, array)
    
       return element > 0;
    
        
    // Driver code
    var arr = [ 11, 89, -23, 7, 98 ]; 
      
    // check for positive number 
    var value = arr.filter(ispositive); 
    console.log( value );
</script>

chevron_right


Output: 



[11,89,7,98]

Example 2: 

JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // check for odd number 
    function isodd(element, index, array) 
    {  
       return (element % 2 == 1);  
    }   
    // Driver code
    var arr = [ 11, 89, 23, 7, 98 ]; 
       
    // check for odd number 
    var value = arr.filter(isodd); 
    console.log( value );
</script>

chevron_right


Output: 

[11,89,23,7]

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.