Tom Tom - 17 days ago 86
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.