Essential Types And Notations 20 exercises
Problem

Arrays of Objects in TypeScript

Let's look at an example of working with arrays of objects.

Consider this processRecipe function which takes in a Recipe type:


type Recipe = {
title: string;
instructions: string;
};
const processRecipe = (recipe: Recipe) => {
// Do something with the recipe in here
};
proce

Loading exercise

Transcript

00:00 In this exercise, we're going to cover arrays again, but we're going to look at arrays of objects. We have a process recipe function, which takes in a type of recipe, and this recipe has a title attribute and it has some instructions as an attribute too. And it also has some ingredients,

00:16 and these ingredients have an array. They're basically an array of ingredients with a name attribute and a quantity attribute. They're using the American style of two cups and one cup here, which I find totally unfathomable, but we'll save that for another day.

00:31 So you have to try to find a way of expressing an array of objects in TypeScript. You can use either of the syntaxes that we covered before with arrays, and make sure you're using the proper object syntax too. And you'll need to edit this type of recipe at the top in order to figure it out. Good luck.