Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialYuting Le
Full Stack JavaScript Techdegree Graduate 20,125 PointsCan someone explain more about this code snippet?
I am confused about the code in this video (https://teamtreehouse.com/library/one-solution-27).
/* GET recipe page. */
router.get('/recipes/:id', function(req, res, next) {
const recipeId = req.params.id;
const recipe = recipes.find( ({ id }) => id === +recipeId );
{ id }: Why id is in the {}?
id === +recipeId: Why there's a + in front of. the recipeId?
1 Answer
Zimri Leijen
11,835 Points({ id }) => id === +recipeId
is a function which is defined in the recipes object. recipes.find apparently passes on object as an argument, but in this case you're only interested in the id.
Therefore you are using object destructuring to get only the id.
The + in front of recipeId will change the type of recipeId from a string to a number. It's a unary plus