Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
having trouble understanding where 'item' comes from in data.map() and what does it represent?
Looking for an explanation on the 'item' argument I'm having trouble understanding where 'item' comes from in data.map() and what does it represent?
Dmitry Polyakov4,964 Points
Item is just a parameter name in a callback function. It can be any word you like.
Manoj Gurung5,317 Points
initially i had a hard time understanding, but here I hope this helps and clarifies your issue.
fetch("https://dog.ceo/api/breeds/list") .then(data=>data.json()) //line1 .then(jason=>generateOptions(jason.message));
in line 1 whatever data you get out of this fetch api. lets name it here "data", then after naming the return data "data", then we set a function that says--> hey lets run .json() method on the data we got back.
Its is like saying this var return of fetch(url)=data// if we omit this line the .json method won't understand on what data are we running .json() method.
i have named it like this for your understanding fetch("https://dog.ceo/api/breeds/list") .then(datawegetbackfromfetch=>datawegetbackfromfetch.json())
now the same goes for .map function
var animals= ["dog", "cat", "mouse"]
const letsaddletterAattheendofthenameofeachanimal= animals.map(eachiteminarray=>
output will be this
["dogA", "catA", "mouseA"]
Extending the question a little bit, could we use data.message inside the function instead of using it in the call?
Not sure if that makes sense, it's just confusing me a bit.