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 Implement a Callback

Understanding the use of the map method in this video

I see that Guil has used the map method to iterate through the functions he has created. But I have to question why he used it. Since we used .map, and it creates a whole new array, what exactly are we going to do with this "array". We created an array that is not being accessed in other parts. I can see how this calls for less lines of code but I'm just curious to see what the left out array would have use for as opposed to a regular loop that is iterating throughout the objects array length.

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

You are correct, as an example of map, this is unfortunately a very poor use-case and actually demonstrates an anti-pattern. For...of or forEach were better alternatives for this. Now at the end of the day nothing is actually going to happen because of this, garbage collection will take care of that unused array, it's just not a good practice to do so.

Yeah it's adding a .map method on top of an already complex topic.

instead of ---

    json.people.map( person => { 
      getJSON(wikiUrl + person.name, generateHTML );
    });

replace with ---

  for (let i = 0; i < json.people.length; i++) {
      getJSON(wikiUrl + json.people[i].name, generateHTML);
    }