TypeScript Array of Objects: Declare, Find, Sort and Group
Declare a typed array of objects with an interface or type alias, then add, find, filter, sort, group and update its objects without surprises.
Declare a typed array of objects with an interface or type alias, then add, find, filter, sort, group and update its objects without surprises.
TypeScript has no Vector class. Model each vector as a tuple such as [x: number, y: number, z: number], store them in an array, and do vector math with typed functions.
The map() method returns one result per element, while flatMap() can return zero, one or many and flattens them one level. Tested examples on TypeScript 7.0.2.
How slice(start, end) copies part of a TypeScript array: excluded end index, negative indexes, pagination and chunks, shallow copies, slice() vs splice(), and result types.
Remove items from a TypeScript array by position or by value, in place or on a copy, with the common bugs (-1 index, splice in a loop, delete) shown and fixed.
Add items to the end, start or any index of a TypeScript array, or to a new array with spread, concat() and toSpliced(), with the compiler errors explained.
Find elements in a TypeScript array with find(), findIndex(), findLast() and findLastIndex(), handle the T | undefined result, and pick the right search method.
Filter an array of objects by property in TypeScript with filter(): single and multiple conditions, lists of values, nested and optional fields, a typed filterBy() helper and type narrowing.
How to declare, type, read, loop over, copy and protect arrays in TypeScript, with tuples and the ES2023 copying methods, tested on TypeScript 7.0 and Node.js 22.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.