What is array filter in JavaScript

In this article I will go to explain use of array filter method in JavaScript
  • 1987

Array filter method in JavaScript

JavaScript array filter method creates a new array with all element that pass the test implemented by the given function.


Syntax of filter method

array.filter(callback[, thisArgs);

 

Parameters in filter method:

  • arrary

    Array required an array object.
     

  • callback

    Callback function accepts up to three arguments, and callback function is used to test  for each element
     

  • thisArgs

    ThisArgs is used as this when executing callbacks.

Return value in filter method

Filter method returned a newly created array.

Exception in filter method

If callback argument is not a function object, A TypeError exception is fire.

There are the following callback function parameter:
 

Parameter Value
value Array element value.
index Numeric  index of the array element.

Example of filter  method

<html>

<head>

<title>Filter method in java scrip</title>

</head>

<body>

<script type="text/javascript">

    var arr = [5, "Filter", 10, "Method", true];

    var result = arr.filter(

    function (value) {

        return (typeof value === 'string');

    }

);

 

    document.write(result);

</script>

</body>

</html>


Output:
 

Filter, Method


Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.