How to Convert an Array to a Dictionary in TypeScript

Convert an array to a dictionary in TypeScript: Command Prompt output of Object.fromEntries building a Record keyed by employee id

The quickest way to convert an array to a dictionary in TypeScript is Object.fromEntries(): map each item to a [key, value] pair and type the result as a Record. Here’s the one-liner I use for an array of objects keyed by id: Every snippet below ran in strict mode on TypeScript 7.0.2 and Node.js v22.22.2, … Read more >>

How to Map an Array of Objects to Another Array in TypeScript

Map an array of objects in TypeScript: Command Prompt output of map() turning Employee objects into EmployeeCard objects

You map an array of objects to another array in TypeScript with Array.prototype.map(). Give the callback a typed input and an explicit return type, and wrap the object literal in parentheses: I compiled every example below in strict mode with TypeScript 7.0.2 and Node.js v22.22.2, and each output block is copied from my console. Map … Read more >>

How to Initialize an Array in TypeScript

Initialize an array in TypeScript: Command Prompt output of typed arrays declared with values, empty and with the generic Array syntax

To initialize an array in TypeScript, declare the element type and give it a value: const cities: string[] = [“Austin”, “Denver”], or const ids: string[] = [] to start empty. Array<string> means the same as string[]. For a fixed number of slots use Array.from({ length: n }, …) rather than new Array(n), which leaves holes. … Read more >>

Check if an Array Is Null or Empty in TypeScript

Check if an array is null or empty in TypeScript: Command Prompt output showing the check against a filled array, an empty array, null and undefined

To check if an array is null or empty in TypeScript, write if (!items || items.length === 0), or the shorter if (!items?.length), which covers null, undefined and [] in one test. The order matters: reading .length before the null check throws at runtime. This guide shows both forms running against all four cases, the … Read more >>

Remove Empty Strings from an Array in JavaScript and TypeScript

Remove empty strings from an array: Command Prompt output of filter(Boolean) and an explicit filter on an array of city names

To remove empty strings from an array, filter them out: values.filter(Boolean) is the short version and values.filter(value => value !== “”) is the explicit one. Both return a new array and leave the original alone. If the array also contains strings that are only spaces or tabs, trim first: values.filter(value => value.trim() !== “”). The … Read more >>

Check if a TypeScript Array Contains a Specific Value

Check if a TypeScript array contains a specific value: Command Prompt output of includes() on an array of cities

To check if a TypeScript array contains a specific value, call includes(): cities.includes(“Denver”) returns true or false. That works for strings, numbers and booleans. For an array of objects it will not, because includes() compares by reference, so you test a property with some() instead: orders.some(o => o.id === “ORD-1001”). This guide covers both, plus … Read more >>

Find the Maximum Value in an Array of Objects in TypeScript

Find the maximum value in an array of objects in TypeScript: Command Prompt output of the highest salary and the employee who earns it

To find the maximum value in an array of objects in TypeScript, decide first what you want back. For just the number, map the property and use Math.max(): Math.max(…employees.map(e => e.salary)). For the whole object that holds it, use reduce() and keep whichever item is larger. Both are one line, and both have an edge … Read more >>

Arrays of Objects in TypeScript: Declare, Type and Use Them

Arrays of objects in TypeScript: Command Prompt output of a Product interface used to type an array and map over it

An array of objects in TypeScript is declared by describing one object and adding []: const products: Product[] = […], where Product is an interface or a type alias. Array<Product> means the same thing. Once the array is typed, the compiler checks every property you write and every property you read, which is the whole … Read more >>

How to Filter an Array of Objects in TypeScript

Filter an array of objects in TypeScript: Command Prompt output of filtering orders by status, total and city

To filter an array of objects in TypeScript, pass filter() a function that tests a property: orders.filter(order => order.status === “shipped”). You get a new array of the same object type, the original is untouched, and the compiler checks every property name as you type it. This guide covers filtering by one property and by … Read more >>