This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.

Check out a free preview of the full JavaScript: From Fundamentals to Functional JS course:
The "_.map() vs. _.each" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS course featured in this preview video. Here's what you'd learn in this lesson:

Because _.each() does not allow values to be returned, it’s better suited for loops that are performing an action. The _.map() method is better for loops that are manipulating data. Bianca discusses this as well as a few other best practices when using these methods.

Get Unlimited Access Now

Transcript from the "_.map() vs. _.each" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Bianca Gandolfo: So then let's talk about the difference between each and map. So the important thing to know is that each like I mentioned before, you can't return anything from each. So, each will be something better suited for getting each animal to speak versus if you wanted to have it return something like especially if you want to return an array.

[00:00:29] Map is a better bet, however if you don't wanted to explicitly return an array maybe you want to return an object. You might wanna wrap each into a function where it pushes to an object instead.
>> Bianca Gandolfo: So then we can run all of our code, or let's get some more names.

[00:00:49] Let's see, Kim, John.
>> Speaker 2: So if I wanted to map that into a, if I wanted to use each to map it into an object, I still have to make the function. But I don't have to iterate because each does that for me and just push it on to that object.

[00:01:12]
>> Bianca Gandolfo: Yeah, exactly, however you wanna implement it. But, map is always gonna return an array.
>> Speaker 2: Right.
>> Bianca Gandolfo: Mm-hm
>> Speaker 3: The prior example had an explicit return on animal maker, why the inference?
>> Bianca Gandolfo: What's do they mean by an explicit return?
>> Speaker 4: When you use map, you have to have return, but when you use each you don't use return.

[00:01:37]
>> Bianca Gandolfo: Yeah. When you use each, you can't use return, each doesn't return anything. But map, you have to use return. Whatever is returned from that callback function is then going to be put into the array. So it's really important that whenever you're using map that you're actually returning something.

[00:01:54] Otherwise your gonna get an array of undefined. So if I didn't return here, we'd get and we still mapped over it, we'd get an array of three undefined values. So we need to make sure that we return in that call back function. Where as the animal, and this is for the animal.speak is fine, because we're not returning anything there, we're just console that logging.

[00:02:19]
>> Speaker 3: They're a little bit behind us, but the prior map example didn't have a return.
>> Bianca Gandolfo: Or maybe did I miss the return?
>> Speaker 5: When we did that one, I think what they're saying is when you did the map with var farm there's no return there.
>> Speaker 5: Maybe that's what they're

[00:02:44]
>> Bianca Gandolfo: Like this? There needs to be a return. So if there wasn't a return, I made a mistake here, AnimalMaker(name). So you have to return here. Cuz what this is saying is, so AnimalMaker return something and then you're returning whatever AnimalMaker returns, so that's why there's two returns here.

[00:03:13]
>> Bianca Gandolfo: So I hope that answers your question.
>> Bianca Gandolfo: Okay, cool. We're right on time again, awesome. So. Just a quick recap each, we can't return anything. Now we have to return something and it returns into an array. Do you have any question Mark?
>> Speaker 3: Yeah, they're the same that by just putting animal maker there that explicitly has a return?

[00:03:50]
>> Bianca Gandolfo: It explicitly has a return inside AnimalMaker.
>> Speaker 3: Right, but on that map, I think that the last example, the short version just had AnimalMaker. So that implicitly does the return, right?
>> Bianca Gandolfo: Yes.
>> Speaker 3: Okay.
>> Bianca Gandolfo: But it's important that you actually explicitly return something inside an animal maker.

[00:04:21] That's the difference.
>> Speaker 5: Animal maker is already returning something you didn't need that intermediate autonomous function to return the value of calling animal feeder because it's already did that.
>> Bianca Gandolfo: Yeah, exactly.