你知道 TypeScript 中的 reduce
数组方法是做什么用的吗?你能提供一个简单的使用示例吗?
我在谷歌上搜索和 TypeScript 语言规范中的文档,但是没有找到像样的解释和示例。
你知道 TypeScript 中的 reduce
数组方法是做什么用的吗?你能提供一个简单的使用示例吗?
我在谷歌上搜索和 TypeScript 语言规范中的文档,但是没有找到像样的解释和示例。
除了其他回答之外,这里需要补充一点。
如果reduce提供了初始值,则有时必须指定其类型,即:
a.reduce(fn, [])
可能需要
a.reduce<string[]>(fn, [])
或者a.reduce(fn, <string[]>[])
实际上,这是JavaScript数组的reduce
函数,而不是TypeScript特有的。
根据文档所述:将一个函数应用于累加器和数组中每个值(从左到右),以将其减少为单个值。
下面是一个示例,它将数组中的值相加:
let total = [0, 1, 2, 3].reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(total);
这段代码应该输出 6
。
0
,否则可能会引起棘手的错误。 - l-portet0
,这可能会引发棘手的错误。 - l-portet有了TypeScript的泛型,你可以做到这样。
class Person {
constructor (public Name : string, public Age: number) {}
}
var list = new Array<Person>();
list.push(new Person("Baby", 1));
list.push(new Person("Toddler", 2));
list.push(new Person("Teen", 14));
list.push(new Person("Adult", 25));
var oldest_person = list.reduce( (a, b) => a.Age > b.Age ? a : b );
alert(oldest_person.Name);
Reduce()是..
它是..
let array=[1,2,3];
function sum(acc,val){ return acc+val;} // => can change to (acc,val)=>acc+val
let answer= array.reduce(sum); // answer is 6
更改至
let array=[1,2,3];
let answer=arrays.reduce((acc,val)=>acc+val);
此外,您可以在以下情况下使用:
let array=[5,4,19,2,7];
function findMax(acc,val)
{
if(val>acc){
acc=val;
}
}
let biggest=arrays.reduce(findMax); // 19
arr = [1, 2, 5, 4, 6, 8, 9, 2, 1, 4, 5, 8, 9]
v = 0
for i in range(len(arr)):
v = v ^ arr[i]
print(value) //6
+1给@JohnnyHK的回答,这是一个标准的JavaScript函数。
我来到这里是因为在输入这个函数时遇到了一些问题,所以我会在这里分享我的发现。如果你使用标准的IDE,如果你点击reduce
函数,你将得到它的类型定义。
/**
* 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(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;
/**
* 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<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
第一个集合用于将数组 T
缩减为 T
值本身。
还有第二种用法,由 @Quentin 提到,就是你可能想将数组 T
缩减为其他类型。我经常看到它被用作:
const keyToValMap = [{key: 'k1', val: 1}].reduce<Record<string, number>>((map, el) => {
map[el.key] = el.val;
return map
}, {})
一个简单的例子,计算数值数组的乘积。我经常忘记的是将参数作为初始累加器传递时使用变量类型
语法。
const product = (nums: number[]): number => nums.reduce((acc: number, v: number): number => acc * v, 1 as number);
alert(product([1, 2, 3, 4]));
a.reduce(fn, [] as string[])
。 - Cuga[22,'foo'] as [number,string]
这样定义一个更复杂的累加器时,就会出现“无法匹配任何重载”错误。我可以使用对象字面量({x:22,y:'foo'}
)来使它工作。有什么想法吗? - Sasgorillaa.reduce((all: string[], v) => {...}, [])
。我认为这是一种更清晰的解决方案,因为它仅定义了一个普通的参数类型,而不是类型转换。 - phil294