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 >>

How to Sort Arrays in TypeScript

Sort arrays in TypeScript: Command Prompt output of sort() with and without a compare function on numbers and strings

To sort an array in TypeScript, call sort() with a compare function: numbers.sort((a, b) => a – b) for numbers and names.sort((a, b) => a.localeCompare(b)) for text. Swap a and b for descending order. Without a compare function sort() turns every element into a string first, which is why [100, 25, 9, 1000, 3] comes … Read more >>

How to Filter Arrays in TypeScript

Filter arrays in TypeScript: Command Prompt output of filter() selecting numbers above a limit and city names beginning with A

To filter an array in TypeScript, call filter() with a function that returns true for the elements you want to keep: temperatures.filter(temp => temp > 80). It returns a new array and never changes the original, so the result of filtering a number[] is a number[]. The one place TypeScript surprises people is removing undefined … Read more >>