The Javascript TypedArray filter() method is used to form a new array that falls under a given criteria from an existing array.
Syntax:
array.filter (function (currentValue, index, arr), thisArg)
Parameters: currentValue: It represents the current element’s value. index: It represents the index position of the current element. arr: It represents the array on which filter() is called. thisArg: It represents this argument for the function.
Returns: A new array with all the elements that satisfy a given condition.
Example 1:
<!DOCTYPE html> <html> <body> <script type="text/javascript"> function func(i) { return i>= "O"; } var Jewels = ["DIAMOND","GOLD","PLATINUM","SILVER"]; var gems =Jewels.filter(func); document.write(gems); </script> </body> </html>
Example 2:
<!DOCTYPE html> <html> <body> <script type="text/javascript"> function isNegative(element, index, array) { return element < 0; } const testArray = new Int8Array([-16, 45, 456, 40, -47]); const negTestArray = testArray.filter(isNegative); document.write(negTestArray); </script> </body> </html>
Please follow and like us: