C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Array filter() methodThe JavaScript array filter() method filter and extract the element of an array that satisfying the provided condition. It doesn't change the original array. SyntaxThe filter() method is represented by the following syntax: array.filter(callback(currentvalue,index,arr),thisArg) Parametercallback - It represents the function that test the condition. currentvalue - The current element of array. index - It is optional. The index of current element. arr - It is optional. The array on which filter() operated. thisArg - It is optional. The value to use as this while executing callback. ReturnA new array containing the filtered elements. JavaScript Array filter() method exampleLet's see some examples of filter() method. Example 1Let's see a simple filter() example to filter the marks of a student. <script> var marks=[50,40,45,37,20]; function check(value) { return value>30; } document.writeln(marks.filter(check)); </script> Output: 50,40,45,37 Example 2Let's see one more array filter() example. <script> function test(element, index, array) { return element>=25; } document.writeln([21,32,21,43].filter(test)); </script> Output: 32,43
Next TopicJavaScript Array
|