Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | typedArray.filter() with Example

  • Last Updated : 14 Feb, 2019

The typedArray.filter() is an inbuilt function in javascript which is used to form a new typedArray with the elements which satisfies the test implemented by the function provided.
Syntax:

typedarray.filter(callback)

Parameters: It takes the parameter “callback” function which checks each element of the typedArray satisfied by the condition provided. Callback function takes three parameters that are specified below-

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • element: It is the value of the element.
  • index: It is the index of the element.
  • array: It is the array which are being traversed.

    Return value: It returns a new typedarray with the elements that satisfies the test.

    JavaScript code to show the working of this function:




    <script>
      
       // Calling isNegative function to check
       // elements of the typedArray
       function isNegative(element, index, array)
       {
        return element < 0;
       }
      
       // Created some typedArrays.
       const A = new Int8Array([ -10, 20, -30, 40, -50 ]);
       const B = new Int8Array([ 10, 20, -30, 40, -50 ]);
       const C = new Int8Array([ -10, 20, -30, 40, 50 ]);
       const D = new Int8Array([ -10, 20, 30, 40, -50 ]);
      
       // Calling filter() function to check condition
       // provided by its parameter
       const a = A.filter(isNegative);
       const b = B.filter(isNegative);
       const c = C.filter(isNegative);
       const d = D.filter(isNegative);
      
       // Printing the filtered typedArray
       document.write(a +"<br>");
       document.write(b +"<br>");
       document.write(c +"<br>");
       document.write(d);
         
    </script>

    Output:

    -10,-30,-50
    -30,-50
    -10,-30
    -10,-50
    My Personal Notes arrow_drop_up
  • Recommended Articles
    Page :