vue(高级函数:filter,map,reduce)

filter

filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

注意: filter() 不会对空数组进行检测。

注意: filter() 不会改变原始数组。

语法

array.filter(function(currentValue,index,arr), thisValue)

详细网址:https://www.runoob.com/jsref/jsref-filter.html

map

map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

map() 方法按照原始数组元素顺序依次处理元素。

注意: map() 不会对空数组进行检测。

注意: map() 不会改变原始数组。

语法

array.map(function(currentValue,index,arr), thisValue)

详细网址:https://www.runoob.com/jsref/jsref-map.html

reduce

reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。

reduce() 可以作为一个高阶函数,用于函数的 compose。

注意: reduce() 对于空数组是不会执行回调函数的。

语法

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

详细网址:https://www.runoob.com/jsref/jsref-reduce.html

案例:给出一组数将其中小于100的数乘于2再求和,下面给出了三种方式



    
        
        
		
        
    
    
        
        
               

你可能感兴趣的:(vue,前端)