你知道reduce
数组方法在TypeScript中的作用吗?
我在Google和the TypeScript language specification上搜索,但找不到任何像样的解释和例子.
你知道reduce
数组方法在TypeScript中的作用吗?
我在Google和the TypeScript language specification上搜索,但找不到任何像样的解释和例子.
它实际上是JavaScript数组reduce
函数,而不是TypeScript特有的东西.
如the docs:Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.所述
下面是一个总结数组值的示例:
let total = [0, 1, 2, 3].reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(total);
这个片段应该产生6
个.