Open In App

Array filter() Method – TypeScript

Last Updated : 10 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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>
    // 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>

Output: 

[11,89,7,98]

Example 2: 

JavaScript
    // 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 );

Output
[ 11, 89, 23, 7 ]

Example 3: In this example, we will see how to use the inline callback function instead of referencing a seperate function.

JavaScript
//Driver code
var arr = [ 11, 89, 23, 7, 98 ];    
// check for odd number 
var value = arr.filter(element => element % 2 == 1); 
console.log( value );

Output
[ 11, 89, 23, 7 ]

Previous Article
Next Article

Similar Reads

PHP Filter and Filter Constant
PHP Filter is an extension that filters the data by either sanitizing or validating it. It plays a crucial role in the security of a website, especially useful when the data originates from unknown or foreign sources, like user-supplied input. For example data from an HTML form. There are mainly two types of filters which are listed below: Validati
3 min read
AngularJS filter Filter
The "filter" Filter in AngularJS is used to filter the array and object elements and return the filtered items. In other words, this filter selects a subset (a smaller array containing elements that meet the filter criteria) of an array from the original array. Syntax: {{arrayexpression | filter: expression : comparator : anyPropertyKey}} Parameter
3 min read
Filter a Dictionary by Key or Value in TypeScript
Filtering a dictionary by key or value is a common task in TypeScript when working with data structures. TypeScript provides various approaches to achieve this efficiently. Table of Content Using Object.keys() and Array.filter()Using Object.entries() and Array.filter()Using the for...in loopUsing reduce MethodUsing Object.keys() and Array.filter()T
3 min read
How to Filter Keys of Type string[] in TypeScript ?
In TypeScript, the filtering of keys of type string[] can be done by iterating over the data object, and applying the condition to the string if the key is a string. If the condition is satisfied, then an array of output results is created which consists of filtered keys of type string[]. The below methods can be used to filter the keys of type str
3 min read
ES6 | Array filter() Method
The Array filter() is an inbuilt method, this method creates a new array with elements that follow or pass the given criteria and condition. Few Examples have been implemented below for a better understanding of the concept Syntax: var newArray = arr.filter(callback(element[, index[, array]]) [, thisArg]) Parameter: This method accepts 2 parameters
2 min read
JavaScript Array filter() Method
JavaScript Array filter() Method is used to create a new array from a given array consisting of only those elements from the given array that satisfy a condition set by the argument method. Syntax: array.filter(callback(element, index, arr), thisValue)Parameters: This method accepts five parameters as mentioned above and described below: callback:
2 min read
How to write a program that takes a predicate and array, like Array.filter(), but only keeps x if pred(x) === false in JavaScript ?
The predicate in JavaScript is the same as the predicate maths which returns true or false. The predicate is just like a function that determines whether the passed arguments satisfy the given condition or not. The predicate returns only true or false. In this article, let us see the working of the predicate with arrays when it is passed with a fun
3 min read
Filter Array of Objects with Another Array of Objects in JavaScript
Filtering an array of objects with another array in JavaScript involves comparing and including objects based on specific criteria. Below are the approaches to filter an array of objects with another array of objects in JavaScript: Table of Content Using filter and includes MethodsUsing LoopingUsing filter and includes MethodsIn this approach, we a
2 min read
TypeScript | Array unshift() Method
The Array.unshift() is an inbuilt TypeScript function that is used to add one or more elements to the beginning of an array and returns the new length of the array. Syntax: array.unshift( element1, ..., elementN ) Parameter: This method accepts n number of similar elements. element1, ..., elementN : This parameter is the elements to add to the fron
1 min read
TypeScript Array toString() Method
The Array.toString() is an inbuilt TypeScript function that is used to get a string representing the source code of the specified array and its elements. Syntax: array.toString() Parameter: This method does not accept any parameter. Return Value: This method returns the string representing the array. Below example illustrate the String toString() m
1 min read