nodejs中reduce

reduce方法介绍

 

reduce 英文意思:v.减少,缩小(尺寸、数量、价格等); (使) 蒸发; 减轻体重; 节食;

vscode中reduce方法提示:

(method) Array<number>.reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number (+2 overloads)
Calls the specified callback function for all the elements in an array.
 The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

@param callbackfn — A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

@param initialValue — If initialValue is specified, it is used as the initial value to start the accumulation. 
The first call to the callbackfn function provides this value as an argument instead of an array value.

数字数组执行reduce方法:

//数组元素一共3个,以下循环2次
//开始循环时previousValue为数组第一个元素,currentValue为数组第二个元素,currentIndex为currentValue值在数组中位置
//再次循环时,previousValue为回调函数上一次返回值,currentValue为数组下一个元素,currentIndex为currentValue值在数组中位置

不带初始值时的执行:

let arry:number[] = [1,2,3];
//数组元素一共3个,以下循环2次
//开始循环时previousValue为数组第一个元素,currentValue为数组第二个元素,currentIndex为currentValue值在数组中位置
//再次循环时,previousValue为回调函数上一次返回值,currentValue为数组下一个元素,currentIndex为currentValue值在数组中位置
arry.reduce((previousValue,currentValue,currentIndex,array)=>{
    console.log(JSON.stringify(previousValue));
    console.log(JSON.stringify(currentValue));
    console.log(JSON.stringify(currentIndex));
    console.log(JSON.stringify(array));
    console.log('------------分割线----------');
    return 1;
})


/*
运行结果:
1
2
1
[1,2,3]
------------分割线----------
1
3
2
[1,2,3]
------------分割线----------
*/

带初始值时的执行:

let arry:number[] = [1,2,3];
//数组元素一共3个,以下循环2次
//开始循环时previousValue为数组第一个元素,currentValue为数组第二个元素,currentIndex为currentValue值在数组中位置
//再次循环时,previousValue为回调函数上一次返回值,currentValue为数组下一个元素,currentIndex为currentValue值在数组中位置
arry.reduce((previousValue,currentValue,currentIndex,array)=>{
    console.log(JSON.stringify(previousValue));
    console.log(JSON.stringify(currentValue));
    console.log(JSON.stringify(currentIndex));
    console.log(JSON.stringify(array));
    console.log('------------分割线----------');
    return 1;
},10)


/*
执行结果:
10
1
0
[1,2,3]
------------分割线----------
1
2
1
[1,2,3]
------------分割线----------
1
3
2
[1,2,3]
------------分割线----------
*/

数字数组实现累加功能:

let arry:number[] = [1,2,3];
let res:number = arry.reduce((previousValue,currentValue,currentIndex,array)=>{
    console.log('------------分割线----------');
    console.log(JSON.stringify(previousValue));
    console.log(JSON.stringify(currentValue));
    console.log(JSON.stringify(array));
    return previousValue+currentValue;
})

console.log('------------打印返回结果----------');
console.log(JSON.stringify(res));

/*
输出结果:

------------分割线----------
1
2
[1,2,3]
------------分割线----------
3
3
[1,2,3]
------------打印返回结果----------
6
*/

实现Promise一直向后then循环(这样就可以让异步代码循环执行起来):

拼凑Promise.then的回调函数数组,为了驱动让Promise一直then调用下去,给一个初始值Promise.resolve()

这样通过reduce函数一直向下执行

//then函数的回调函数不带任何参数
let arry:any[] = [()=>{
    return Promise.resolve(1);
},
()=>{
    return Promise.resolve(2);
},
()=>{
    return Promise.resolve(3);
}];

let res:Promise<number> = arry.reduce((previousValue,currentValue,currentIndex,array)=>{
    return previousValue.then(currentValue).then((data:number)=>{
        return data;
    });
},Promise.resolve())

res.then(data=>{
    console.log('------------打印返回结果----------');
    console.log(JSON.stringify(data));
});

/*
输出结果:
------------打印返回结果----------
3
*/
//then函数的回调函数加上参数
let arry:any[] = [(d:number)=>{
    return Promise.resolve(d+1);
},
(d:number)=>{
    return Promise.resolve(d+2);
},
(d:number)=>{
    return Promise.resolve(d+3);
}];

let res:Promise<number> = arry.reduce((previousValue,currentValue,currentIndex,array)=>{
    return previousValue.then(currentValue).then((data:number)=>{
        return data;
    });
},Promise.resolve(10))

res.then(data=>{
    console.log('------------打印返回结果----------');
    console.log(JSON.stringify(data));
});

/*
输出结果:
------------打印返回结果----------
16
*/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值