Transcript
00:00 Hey. Welcome to the array methods exercise. I'm excited to teach you all about the methods that I use most with arrays and that is map filter and reduce. And reduce a little bit less now that we have really good, for loops with the for of, syntax. We didn't always have that.
00:17 Reduce was a lot more useful, before that showed up. But now, I find myself using for of loops a lot more. But, reduce is still quite helpful. So, map, the basic idea of that is to take an array and map it to another array that has all the same or has each entry of that original array mapped to some other value. So here's an example of that.
00:37 You got your numbers. You're going to map those to the times two of each one of those numbers. So one, two, three, now that's two, four, six. And then filter is, a way to take an array and remove some items from that array and get a result of, those items. So here's the even numbers of those items.
00:54 We're gonna use the modulus operator. So n modulus two equals zero. That will tell you whether or not that number is zero or is, even. And so if it is, if this value if this expression returns true then, that gets included in the new array that's being created. So we'll talk about that and that's gonna be great.
01:15 And then reduce is a little bit more complicated of these three and that involves this accumulator value. And so whatever, you return from this function is going to be set to the accumulator and then it'll call the function again for the next item in the array and continue doing that, until that accumulator is done going through all the items of the array and whatever that accumulator equals at the end is what your resulting number, value is. And so what that means is that your reduce can actually take an array and transform it into something completely different. Where map, just takes an array and gives you a new array and filter takes an array and gives you a new array. Reduce takes an array and turns it into anything else and you get a lot of control over that.
01:57 It's a little bit complicated but, it is pretty powerful and it's definitely useful. And then you can kinda mix and match all these things with chaining. So you can take these products, you filter on it, and then take the results of that, and you map on that, and then take the results of that, and then reduce on that. It's pretty cool. We're gonna talk about that too.
02:13 So I'm excited to have you join me for this exercise. Let's get into it.
