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

JavaScript Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Async Programming and Callback Functions

clarification on array.map()

btn.addEventListener('click', (event) => {
  getJSON(astrosUrl, (json) => {
    json.people.map( person => {
      getJSON(wikiUrl + person.name, generateHTML);
    })
  });
  event.target.remove();
});

.map() returns an array? What will happen without using it

1 Answer

Austin Whipple
Austin Whipple
29,725 Points

The .map() method will create an array containing the results of the a function run on every item in the array on which .map() was called. In this case, .map() will iterate over all of the items in the people array and then run the getJSON() function every time, which then retrieves the JSON data and passes it on to the generateHTML() function.

WIthout the .map() method here, you could probably still achieve the same result, but you'd probably have to write a couple of loops to do so.

More on Map is available in the MDN docs.

alright thank you