How to Use the Array reduce() Method in TypeScript

TypeScript array reduce method: Command Prompt output tracing the accumulator at every step

The array reduce() method in TypeScript runs a callback on each element and carries one value, the accumulator, from step to step, so an array becomes a single result. Pass an initial value as the second argument; its type decides what reduce() returns: All of the output on this page is real: TypeScript 7.0.2 plus … Read more >>

How to Flatten an Array of Arrays in TypeScript?

Flatten an Array of Arrays in TypeScript

In this tutorial, I will explain how to flatten an array of arrays in TypeScript. As a TypeScript developer, if you are working with nested data structures, then you will work with flattening an array. Let me explain various ways to do this with some examples. What Is Array Flattening in TypeScript? Array flattening in … Read more >>

How to Concatenate Arrays in TypeScript Using concat()

Concatenate arrays in TypeScript: Command Prompt output of joining two order arrays with concat()

concat() is the built-in way to concatenate arrays in TypeScript: a.concat(b) returns a new array with every item of a followed by every item of b, and neither original changes. The spread syntax […a, …b] does the same job: Every snippet was compiled and run with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so … Read more >>

How to Convert a Map to an Array in TypeScript

TypeScript map to array: Command Prompt output of a Map converted to entries, keys and values arrays

To convert a Map to an array in TypeScript, spread it: […map] gives you an array of [key, value] pairs. For just the keys or the values, spread map.keys() or map.values(), or use Array.from(): The examples use a small coffee shop inventory, and they were compiled and run with TypeScript 7.0.2 on Node.js v22.22.2. We’ll … Read more >>

How to Create a Map from an Array in TypeScript

TypeScript create map from array: Command Prompt output of a Map built from an array of customer objects

You can create a Map from an array in TypeScript by passing an array of [key, value] pairs to new Map(). For an array of objects, turn each object into a pair with map() first, and TypeScript infers the key and value types for you: These snippets were checked with TypeScript 7.0.2 with Node.js v22.22.2 … Read more >>

TypeScript 2D Arrays: Create, Loop and Type Them with Interfaces

TypeScript 2D array guide: Command Prompt output comparing a fill() grid that shares one row with an Array.from grid

A 2D array in TypeScript is an array of arrays, typed number[][] or Array<Array<number>>. Each inner array is a row, and you read a cell with two indexes, row first: sales[1][2]. The outputs below came straight out of the Windows Command Prompt, compiled with tsc –strict on TypeScript 5.2.2 and Node.js v22.22.2. You’ll build empty … Read more >>

How to Find an Object in an Array in TypeScript

Find an object in an array in TypeScript: Command Prompt output of tsc and node finding a product object by id and name with Array.find

To find an object in an array in TypeScript, use the array find() method with a condition on a property: products.find(p => p.id === 102) returns the first matching object, or undefined if there is none. TypeScript types the result as Product | undefined, so you must handle the missing case. This guide covers TypeScript … Read more >>

How to Sum an Array in TypeScript (Numbers and Objects)

TypeScript sum array: Command Prompt output of summing an array of objects with reduce

To get the sum of an array in TypeScript, call reduce() with an initial value of 0: it adds every element to a running total and returns a number. Always pass that 0, or an empty array will throw: I compiled each example with TypeScript 7.0.2 and Node.js v22.22.2 using strict mode, and the outputs … Read more >>

How to Clear an Array in TypeScript While Preserving Its Type?

Clear an array in TypeScript while preserving its type: Command Prompt output of splice emptying a Person array

Today, we will discuss an interesting topic in TypeScript. In this tutorial, I will explain how to clear a typed array in TypeScript while preserving its original type. Clear an Array in TypeScript While Preserving Its Type Let’s say we have an array of a custom type called Person in our TypeScript code: Now, suppose … Read more >>