Home > Article > Web Front-end > What is the difference between filter and map in es6
The difference between filter and map in es6: the new array returned by the map method is a mapping of the original array. The new array has the same length as the original array but different values; while the new array returned by the filter method is the filtered version of the original array. , the length of the new array is different from the original array, but the value remains unchanged.
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
The map and filter parameters are exactly the same
array.filter(function(currentValue,index,arr), thisValue)
currentValue: array element;
index: index
arr: original array;
thisValue: used as the execution callback, passed to Function, used as the value of "this"
Different uses:
1. The new array returned by the map method is a mapping of the original array. What is mapping? It is the same length as the original array, and the values are processed accordingly.
2. The value returned by the filter method is the new array after filtering the original array. The length is different from the original array, but the value remains unchanged.
Examples are as follows:
filter:
Syntax:
arr.filter(function(val,index,arr){})
It can be concluded from this:
filter will remove all false data, in other words, filter out data that meets the conditions.
map:
Syntax:
arr.map(function(val,index,arr){})
Use the same code to implement and see what the result is
So map is generally suitable for calculation and can return calculation results.
But when a conditional judgment is encountered, it will only return whether it is true or false.
【Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of What is the difference between filter and map in es6. For more information, please follow other related articles on the PHP Chinese website!