The filter()
method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
The filter()
method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
// Arrow function filter((element) => { /* … */ } ) filter((element, index) => { /* … */ } ) filter((element, index, array) => { /* … */ } ) // Callback function filter(callbackFn) filter(callbackFn, thisArg) // Inline callback function filter(function(element) { /* … */ }) filter(function(element, index) { /* … */ }) filter(function(element, index, array){ /* … */ }) filter(function(element, index, array) { /* … */ }, thisArg)
callbackFn
Function is a predicate, to test each element of the array. Return a value that coerces to true
to keep the element, or to false
otherwise.
The function is called with the following arguments:
element
The current element being processed in the array.
index
The index of the current element being processed in the array.
array
The array on which filter()
was called.
thisArg
Optional
Value to use as this
when executing callbackFn
.
A shallow copy of a portion of the given array, filtered down to just the elements from the given array that pass the test implemented by the provided function. If no elements pass the test, an empty array will be returned.
filter()
calls a provided callbackFn
function once for each element in an array, and constructs a new array of all the values for which callbackFn
returns a value that coerces to true
. callbackFn
is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. Array elements which do not pass the callbackFn
test are skipped, and are not included in the new array.
callbackFn
is invoked with three arguments:
If a thisArg
parameter is provided to filter
, it will be used as the callback's this
value. Otherwise, the value undefined
will be used as its this
value. The this
value ultimately observable by callbackFn
is determined according to the usual rules for determining the this
seen by a function.
filter()
does not mutate the array on which it is called.
The range of elements processed by filter()
is set before the first invocation of callbackFn
. Elements which are assigned to indexes already visited, or to indexes outside the range, will not be visited by callbackFn
. If existing elements of the array are deleted in the same way they will not be visited.
Warning: Concurrent modification of the kind described in the previous paragraph frequently leads to hard-to-understand code and is generally to be avoided (except in special cases).
The following example uses filter()
to create a filtered array that has all elements with values less than 10
removed.
function isBigEnough(value) { return value >= 10; } const filtered = [12, 5, 8, 130, 44].filter(isBigEnough); // filtered is [12, 130, 44]
The following example returns all prime numbers in the array:
const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; function isPrime(num) { for (let i = 2; num > i; i++) { if (num % i === 0) { return false; } } return num > 1; } console.log(array.filter(isPrime)); // [2, 3, 5, 7, 11, 13]
The following example uses filter()
to create a filtered JSON of all elements with non-zero, numeric id
.
const arr = [ { id: 15 }, { id: -1 }, { id: 0 }, { id: 3 }, { id: 12.2 }, {}, { id: null }, { id: NaN }, { id: 'undefined' }, ]; let invalidEntries = 0; function filterByID(item) { if (Number.isFinite(item.id) && item.id !== 0) { return true; } invalidEntries++; return false; } const arrByID = arr.filter(filterByID); console.log('Filtered Array\n', arrByID); // Filtered Array // [{ id: 15 }, { id: -1 }, { id: 3 }, { id: 12.2 }] console.log('Number of Invalid Entries = ', invalidEntries); // Number of Invalid Entries = 5
Following example uses filter()
to filter array content based on search criteria.
const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange']; /** * Filter array items based on search criteria (query) */ function filterItems(arr, query) { return arr.filter((el) => el.toLowerCase().includes(query.toLowerCase())); } console.log(filterItems(fruits, 'ap')); // ['apple', 'grapes'] console.log(filterItems(fruits, 'an')); // ['banana', 'mango', 'orange']
The following example tests the behavior of the filter
method when the array is modified.
// Modifying each word let words = ['spray', 'limit', 'exuberant', 'destruction', 'elite', 'present']; const modifiedWords = words.filter((word, index, arr) => { arr[index + 1] += ' extra'; return word.length < 6; }); console.log(modifiedWords); // Notice there are three words below length 6, but since they've been modified one is returned // ["spray"] // Appending new words words = ['spray', 'limit', 'exuberant', 'destruction', 'elite', 'present']; const appendedWords = words.filter((word, index, arr) => { arr.push('new'); return word.length < 6; }) console.log(appendedWords); // Only three fits the condition even though the `words` itself now has a lot more words with character length less than 6 // ["spray" ,"limit" ,"elite"] // Deleting words words = ['spray', 'limit', 'exuberant', 'destruction', 'elite', 'present']; const deleteWords = words.filter((word, index, arr) => { arr.pop(); return word.length < 6; }) console.log(deleteWords); // Notice 'elite' is not even obtained as it's been popped off 'words' before filter can even get there // ["spray" ,"limit"]
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
filter |
1 |
12 |
1.5 |
9 |
9.5 |
3 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter