Home >Web Front-end >Front-end Q&A >What is the difference between filter and map in es6

What is the difference between filter and map in es6

WBOY
WBOYOriginal
2022-03-31 10:20:303030browse

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.

What is the difference between filter and map in es6

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

What is the difference between filter and map in es6

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){})

What is the difference between filter and map in es6

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

What is the difference between filter and map in es6

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn