Home >Web Front-end >Front-end Q&A >What are the parameters of the es6 filter method?
The filter method accepts two parameters: 1. A callback function, which cannot be omitted. It is used to set conditions to filter array elements and return elements in the array that meet the conditions. The syntax is "function (current value, current index, Array object){...}"; 2. An optional parameter, which can be the object using the this keyword in the callback function.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
The filter() method creates a new array. The elements in the new array are checked by checking all elements in the specified array that meet the conditions.
To put it simply: The filter() method can return elements in the array that meet specified conditions.
The filter() method accepts two parameters:
array.filter(callbackfn[, thisArg]);
callbackfn: callback function, cannot be omitted, used to set conditions to filter array elements , accepts up to 3 parameters, syntax:
function(currentValue, index,arr)
currentValue is required. The value of the current element
#index is optional. The index value of the current element
#arr Optional. The array object to which the current element belongs
For each element in the array, the filter method will call the callbackfn function once.
thisArg: Optional parameter, an object that can be used with the this keyword in the callbackfn function. If thisArg is omitted, undefined will be used as the this value.
The return value is a new array containing all the values for which the callback function returns true. If the callback function returns false for all elements of array , the length of the new array is 0.
For each element in the array, the filter method calls the callbackfn function once (in ascending index order). This callback function is not called for missing elements in the array. The usage of callback function is the same as map.
In addition to array objects, the filter method can be used by any object that has a length property and has a numerically indexed property name.
Example 1: Filter out the prime numbers in the array
var a = [31,33,35,37,39,41,43,45,57,49,51,53]; var a1 = a.filter(function(value, index, ar) { high = Math.floor(Math.sqrt(value)) + 1; for (var div = 2; div <= high; div ++) { if (value % div == 0) { return false; } } return true; } ); console.log(a1);//31,37,41,43,53 </script>
Example 2: Filter out the specified range in the array External elements
var f = function (value) { if (typeof value !== 'number'){ return false; } else { return value >= this.min && value <= this.max; } } var a = [6, 12, "15", 16, "the", -12]; var obj = {min : 10, max : 20}; var r = a.filter(f, obj); console.log(r); //12,16
[Related recommendations: javascript video tutorial, web front-end]
The above is the detailed content of What are the parameters of the es6 filter method?. For more information, please follow other related articles on the PHP Chinese website!