js filter, map and reduce

Filter an array to the desired number

Requirements 1. Find out the ones less than 100; 2 find out *2; 3 add the numbers together

The callback function in filter has a requirement that it must return a boolean value.

ture: The function automatically puts this callback function into a new array

false: When false is returned, the function will automatically filter out n

The array will be traversed, and the values ​​​​of the array obtained by n respectively

Multiply by 2 (map has other usages)

 

 add up

reduce summarizes all the values ​​​​of the array and iterates

//The first time tem=0, n=80;

// The second time tem=80 is the above 0+80; n=100;

// The third time tem=180, n=160;

// The third time tem=340, n=180;

// The third time tem=520, n=120;

// tem=640

more succinct way of writing

 Simpler way of writing, arrow function

 let a=arr.filter(n=>n<100).map(n =>n*2).reduce((pre,n)=>pre+n);

Guess you like

Origin blog.csdn.net/ZHUzhuzhu08/article/details/121090014