Tom*_*Tom 36 arrays typescript
你知道数组reduce函数在TypeScript中做了什么吗?你能提供一个简单的使用例子吗?
我搜索Google和TypeScript语言规范,但找不到任何合适的解释和示例.
Joh*_*yHK 42
它实际上是JavaScript数组reduce函数,而不是TypeScript特有的东西.
如文档中所述: 对累加器应用函数和数组的每个值(从左到右)将其减少为单个值.
这是一个TypeScript示例,它总结了数组的值:
let total = [0, 1, 2, 3].reduce((accumulator, currentValue) => accumulator + currentValue);
alert(total);
Run Code Online (Sandbox Code Playgroud)
该alert框将显示6.
Que*_*n 2 26
除了其他答案外,还有一个注释.
如果提供初始值以减少,那么有时必须指定其类型,即: -
a.reduce(fn, [])
Run Code Online (Sandbox Code Playgroud)
可能必须
a.reduce<string[]>(fn, [])
Run Code Online (Sandbox Code Playgroud)
要么
a.reduce(fn, <string[]>[])
Run Code Online (Sandbox Code Playgroud)
how*_*dlo 12
使用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);
Run Code Online (Sandbox Code Playgroud)
减少()是..
它是 ..
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
Run Code Online (Sandbox Code Playgroud)
改成
let array=[1,2,3];
let answer=arrays.reduce((acc,val)=>acc+val);
Run Code Online (Sandbox Code Playgroud)
你也可以在
let array=[5,4,19,2,7];
function findMax(acc,val)
{
if(val>acc){
acc=val;
}
}
let biggest=arrays.reduce(findMax); // 19
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
@JohnnyHK 的 +1 回答是它是一个标准的 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;
Run Code Online (Sandbox Code Playgroud)
第一组用于将数组减少T为T值本身。
还有@Quentin 提到的第二种用法,即您可能希望将数组减少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(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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75565 次 |
| 最近记录: |