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 trialJivansh Bakshi
2,564 PointsHow did the data.quote.find method worked ??
Can somebody help me understand this method const quote = data.quotes.find(quote => quote.id == req.params.id);
1 Answer
Blake Larson
13,014 PointsWhen you send a request to app.get(“/quotes/:id”, (req,res) => {}
That req variable in the callback has a params property equal to whatever the :id is that was passed in from whoever is accessing that endpoint. In the data object there is an array called quotes. So that’s the data.quotes. find() is an array function that loops over that array and returns the first found instance of the condition you enter in the function. So find(quote => quote.id === req.params.id)
If you do it statically you can do app.get(“/quotes/8721”, (req, res) => { const quote = data.quotes.find(quote => quote.id === 8721); The quote variable is now the quote object with the id of 8721. So you can do console.log(quote.author); “Martin Luther King”
Sorry about my format I’m on mobile.
Jivansh Bakshi
2,564 PointsJivansh Bakshi
2,564 PointsNo problem and thnx for the reply