TypeScript和数组减少功能

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.

  • 它是否正确?VS 中的打字稿文档和智能感知表示其前一个元素而不是累加器。 (4认同)
  • @伊万是的。如果您发现文档将其称为“先前”而不是累加器,则它指的是从上次调用减速器函数返回的累加器的值。 (2认同)

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)

  • 我认为语法现在已更改为`a.reduce(fn, [] as string[])` (10认同)
  • 接受的答案省略了解释累加器可以用任何对象初始化,因此也可以是不同类型. (2认同)

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)


Ene*_*rgy 7

减少()是..

  • reduce() 方法将数组缩减为单个值。
  • 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
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)

你也可以在

  1. 找到最大值
    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)
  1. 找到一个不重复的元素
    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)


Kan*_*gur 5

@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)

第一组用于将数组减少TT值本身。

还有@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)