Vue的高级语法

语法:集合.reduce((前一次回调函数的返回值,当前项) => 方法体,前一次回调函数的返回值参数的初始值 )

作用:reduce()函数主要用于对数组进行迭代和转换。

//对数组进行求和:
corst rumbers = [1,2,3,4,5];
const sum = numbers.reduce(function(total,currentvalue){
    return total + currentvalue;
}) ;
conso1e.log(sum) ; //输出15


//箭头函数的数组进行求和:
const sum2 = numbers.reduce((total,currentvalue) => total + currentvalue, 0)


//对数组进行计数:
const fruits = ['app1e', 'banana', ' orange', 'app1e', 'watermelon'];
const count = fruits.reduce(function(tota1,currentvalue) {
if (currentvalue in tota1){
tota1[currentvalue]++;
} else {
tota1[currentvalue] = 1;
l
return tota1 ;
},{});
console.1og(count) ; //输出 {app1e: 2, banana: 1, orange: 1,watermelon: 1}

//对数组进行过滤:
const numbers = [1,2,3,4,5];
const filtered = numbers.reduce(function(total,currentvalue){
if (currentvalue % 2=== 0) tota1.push(currentvalue) ;
return total ;
},[]);
conso1e.1og(filtered); //输出 [2,4]

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值