因为
出自ES6的 Array,用来做累加的。以前我一直用来做算术求和,直到前一阵子看到了一个骚操作,于是又做了一番折腾…
看这段代码先:
const names = ["jack", "pony", "tony", "thomas"];
const entry = names.reduce((prev, cur) => {
prev[cur] = `My full name is ${cur} ma.`;
return prev;
}, {});
结果:
{ "jack": "My full name is jack ma.", "pony": "My full name is pony ma.", "tony": "My full name is tony ma.", "thomas": "My full name is thomas ma." }
把一个数组转成了对象,如此优雅的操作。试想,不用这个如何达到效果!?
const names = ['jack', 'pony', 'tony', 'thomas'];
const obj = {};
for (let name of names) {
obj[name] = `My full name is ${name} ma.`
}
此时,多定义了一个变量,用了一层循环,看起来很没科技感。戳到 API,我们可以看到它是这么定义的:
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;
是的,第二个参数竟然是泛型。之前一直传数字做累加,今天记住了。
所以
reduce 功能不止于此…