Categories
TypeScript Answers

How to take object out of array based on attribute value with TypeScript?

Spread the love

To take object out of array based on attribute value with TypeScript, we use the 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 call array.find with a callback that finds the first object in array that has id equal to 1.

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 *