Categories
TypeScript Answers

How to find an array item with TypeScript?

Spread the love

Sometimes, we want to find an array item with TypeScript.

In this article, we’ll look at how to find an array item with TypeScript.

How to find an array item with TypeScript?

To find an array item with TypeScript, we can use the array find method.

For instance, we write

const array = [
  { id: 1, value: "itemname" },
  { id: 2, value: "itemname" },
];

const item1 = array.find((i) => i.id === 1);

to get the item with id 1 from array and return it with

array.find((i) => i.id === 1)

We call find with a callback that returns the condition of the item we’re looking for in array.

Conclusion

To find an array item with TypeScript, we can use the array find method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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