Skip to content

Instantly share code, notes, and snippets.

@dewey92
Last active October 19, 2018 21:00
Show Gist options
  • Select an option

  • Save dewey92/97f3c93e4c81af9511e978571227dff2 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/97f3c93e4c81af9511e978571227dff2 to your computer and use it in GitHub Desktop.
Array find
interface ProductModel {
id: number;
name: string;
price: number;
tax: number
}
const productList: ProductModel[] = [
{ id: 100, name: 'deluxe', price: 28, tax: 2.5 },
{ id: 101, name: 'giftcard', price: 30, tax: 3 },
{ id: 102, name: 'vase', price: 4.5, tax: 1.25 },
...
];
const getProductById = (products: ProductModel[], id: number) =>
products.find(product => product.id === id);
const product = getProductById(productList, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment