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.

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,686 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