October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Find an Object in an Array in TypeScript

Learn how to find an object by a property in a TypeScript array, safely handle no match, and use filter when you need multiple results.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Array.find() to get the first object that matches a condition. Because no match produces undefined, check the result before reading its properties.

Find an object by a property

For example, to find the user whose id is 2:

type User = {
  id: number;
  name: string;
};

const users: User[] = [
  { id: 1, name: "Ada" },
  { id: 2, name: "Grace" },
];

const targetId = 2;
const user = users.find((candidate) => candidate.id === targetId);

if (user !== undefined) {
  console.log(user.name);
} else {
  console.log("No user found");
}

The callback receives each array element; its condition compares the object’s id with the target. find is the appropriate choice when you want one matching element. TypeScript’s Everyday Types handbook shows this kind of lookup and notes that the result may be undefined if nothing matches.

Handle the missing result

With strictNullChecks enabled, TypeScript flags attempts to access a property on a result that might be undefined. Narrow the result first with a check such as user !== undefined, as in the example. This makes the no-match case explicit and lets TypeScript know the object exists inside that branch.

This handles a missing match, not a missing array. The example assumes users is an array; if the variable holding the array can itself be nullish, handle that input separately before calling find.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a different method when you need a collection

If the goal is every matching object rather than one, use filter, which returns a collection of matches. For union-typed arrays, the TypeScript narrowing handbook demonstrates passing a user-defined type guard to filter so the resulting array is narrowed to the guarded member type.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

If TypeScript does not recognize find

The TSConfig lib reference lists array.find among the APIs available through the ES2015 library definitions. If the compiler reports that find is unavailable, inspect the project’s lib configuration and runtime environment. A library declaration tells the type checker about an API; it does not add a polyfill to a runtime that lacks the method.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

TypeScript 5.2 also documents typing changes for methods such as filter and find on certain unions of arrays: results can be an array of the union element type instead of preserving the original union-of-arrays shape. This is a version-specific note for union-array typing, not a special setup requirement for an ordinary User[] lookup. See the TypeScript 5.2 release notes.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.