JavaScript Iterator filter()
Example
Return an iteratorarray of all values in ages[] that are 18 or over:
// Create an iterator
const myIterator = Iterator.from([32, 33, 16, 40]);
// Filter the iterator
const filteredIterator = myIterator.filter(x => x > 18);
Try it Yourself »
Description
The filter()
method creates a new iterator with elements that pass a test provided by a function.
The filter()
method does not execute the function for empty elements.
The filter()
method does not change the original iterator.
Syntax
iterator.filter(function(currentValue, index, arr), thisValue)
Parameters
Parameter | Description |
function() | Required. A function to run for each iterator element. |
currentValue | Required. The value of the current element. |
index | Optional. The index of the current element. |
arr | Optional. The iterator of the current element. |
thisValue | Optional. Default undefined A value passed to the function as its this value. |
Return Value
Type | Description |
Iterator |
An iterator of elements that pass the test. An empty iterator if no elements pass the test. |
Browser Support
iterator.filter()
is an ES2025 feature.
ES2025 is fully supported in all modern browsers since May 2025:
Chrome 135 | Edge 135 | Firefox 129 | Safari 18.2 | Opera 120 |
Apr 2025 | Apr 2025 | Aug 2024 | Des 2024 | May 2025 |