TypeScript 和数组归约函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14087489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:58:08  来源:igfitidea点击:

TypeScript and array reduce function

arraystypescript

提问by Tom

Do you know what does array reducefunction do in TypeScript? Can you provide a simple example of usage?

你知道reduceTypeScript中的数组函数是做什么的吗?你能提供一个简单的用法示例吗?

I search on Google and TypeScript language specificationbut could not find any decent explanation and examples.

我在 Google 和TypeScript 语言规范上搜索,但找不到任何像样的解释和示例。

回答by JohnnyHK

It's actually the JavaScript array reducefunction rather than being something specific to TypeScript.

它实际上是 JavaScript 数组reduce函数,而不是特定于 TypeScript 的东西。

As described in 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.

文档所述对累加器和数组的每个值(从左到右)应用函数以将其减少为单个值。

Here's an example which sums up the values of an array:

这是一个汇总数组值的示例:

let total = [0, 1, 2, 3].reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(total);

The snippet should produce 6.

该片段应生成6.

回答by Quentin 2

Just a note in addition to the other answers.

除了其他答案之外,只是一个注释。

If an initial value is supplied to reduce then sometimes its type must be specified, viz:-

如果提供初始值以减少,则有时必须指定其类型,即:-

a.reduce(fn, [])

may have to be

可能必须是

a.reduce<string[]>(fn, [])

or

或者

a.reduce(fn, <string[]>[])

回答by howardlo

With TypeScript generics you can do something like this.

使用 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);

回答by Energy

Reduce()is..

减少()是..

  • The reduce() method reduces the array to a single value.
  • The reduce() method executes a provided function for each value of the array (from left-to-right).
  • The return value of the function is stored in an accumulator (result/total).
  • reduce() 方法将数组缩减为单个值。
  • reduce() 方法为数组的每个值(从左到右)执行提供的函数。
  • 函数的返回值存储在累加器中(结果/总数)。

It was ..

它是 ..

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

Change to

改成

let array=[1,2,3];
let answer=arrays.reduce((acc,val)=>acc+val);

Also you can use in

你也可以在

  1. find max
  1. 找到最大值
    let array=[5,4,19,2,7];
    function findMax(acc,val)
    {
     if(val>acc){
       acc=val; 
     }
    }

    let biggest=arrays.reduce(findMax); // 19
  1. find an element that not repeated.
  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