Robert Robinson2,365 Points
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,959 Points
Item is just a parameter name in a callback function. It can be any word you like.
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.
Manoj Gurung5,312 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"]