Tom Tom - 3 years ago 750
TypeScript Question

TypeScript and array reduce function

Do you know what does array

function do in TypeScript?
Can you provide a simple example of usage?

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

Answer Source

It's actually the JavaScript array reduce function rather than being something specific to 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 a TypeScript example which sums up the values of an array:

total = [0, 1, 2, 3].reduce((a, b) => a + b);

The alert box will show 6.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download