The difference between filter, replace and map in js

The three functions traverse each element in the array, very similar

The structures are:

函数((参数)=>{
    
    
    return ...
})

But there are some subtle differences:

The return of filter is a judgment condition, it must be a boolean value, and finally returns a new array

The return of map is an expression, which performs related operations on the value of each element of the array, and finally returns a new array

The return of reduce is also an expression, but each step adds the value of the current element to the calculation result of the previous step

var numbers = [65, 44, 12, 4];
 
numbers.reduce(function getSum(total, num) {
    
    
    return total + num;
})

//结果125,默认初始值为0

numbers.reduce(function getSum(total, num) {
    
    
    return total + num;
},10)
//结果135,因为设置了初始值为10

Supongo que te gusta

Origin blog.csdn.net/m0_58768224/article/details/130036513
Recomendado
Clasificación