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 trial

APIs Displaying the Content

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?

3 Answers

Dmitry Polyakov
Dmitry Polyakov
4,989 Points

Item is just a parameter name in a callback function. It can be any word you like.

Manoj Gurung
Manoj Gurung
5,318 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=> ${eachiteminarray}A)

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.