Check out a free preview of the full Functional-Lite JavaScript course:
The "List Exclusion" Lesson is part of the full, Functional-Lite JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

The Array.filter() method is used to create list exclusions. Each item in the list is passed to a function which returns a boolean value representing whether that item should be included in the list or not. A new list of these filtered items is returned, leaving the original list unmodified.

Get Unlimited Access Now

Transcript from the "List Exclusion" Lesson

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

[00:00:03]
>> Kyle Simpson: Let's talk about exclusion. I take the list and there are certain items that I wanna keep in the list, and certain items that I want to exclude from the list. I want to filter those items out. You might end up with a list that has a whole bunch of numbers in it and in certain places because of whatever you were using to create the list of numbers you might have some no values.

[00:00:26] Or some undefined values and you wanna filter those things out cuz you only wanna produce you operations across a list of real numbers. So you have some things that you wanna filter out.
>> Kyle Simpson: You may wanna filter out anything that's not an odd number. When I filter out the evens by saying my predicate isOdd.

[00:00:45] So here, line 1, my isOdd function takes advantage of the mathematical modulus. Modulus 2 is gonna return 1 for an odd number, 0 for an even number. So what I'm saying here is if the number returns 1, then my expression on line one there, that expression returns true.

[00:01:08] So the way that we're gonna create an exclude function is, you want your predicate, your function, should always return true if you wanna keep it, and non true, false, if you don't wanna keep it. So you're making some decision based only on that value. This is important. You don't have any other context by which to make that decision, other than the value itself.

[00:01:32] That's when a filter applies. if you have to make the decision about inclusion versus exclusion based upon more complex pieces of data than just the value itself You're gonna reach for a different tool than filter. Okay, we'll talk about that in a little bit. But if all we need to do is look at the value itself and decide whether or not we want to keep it, then we can simply use a utility like exclude here.

[00:01:58] Exclude looks an awful lot like our transformed it before it takes in an array, takes in a predicate function called fn. You'll notice that I'm producing a new list. Again, this is an immutable operation, immutable principled operation. So I create a new list and that new list has potentially fewer items in it, maybe even none.

[00:02:22] So I only push into the list, on line seven, I only push into the list if the function that I call with that value returns me a true value. So line six, I called the function that you've passed in. I give it the value that I'm currently iterating over.

[00:02:39] And if you give me a true value back, I'm gonna include it, otherwise I'm going to leave it out. Pretty straightforward. So we call exclude down here at the bottom and we end up with an array list of 1, 3, 5. Questions about our exclude utility? Now, could you do exclude as a recursion?

[00:03:05] Think about that for a moment, I bet you could, well I hope you could, I want to assign that as a formal exercise, but maybe over the next break, think about these operations. I'm doing them in an iterative format with for loops because it's very illustrative, but how would you do it recursively?

[00:03:25]
>> Kyle Simpson: All right. So we're gonna exclude anything that's not odd. We're gonna exclude the evens from our operation.
>> Kyle Simpson: Turns out this exclusion, this filtering is built in a language by a way of the method called filter. So we don't have to write our own, it's available to us.

[00:03:47] So now we have map and we have filter. Now that you've seen the implementations of them, it makes a little bit more sense to you how to use them, right? By the way, just as a little side note, my implementation of exclude and transform so far. Here's a little bit of corner cutting for illustrative purposes.

[00:04:09] They are not exactly the same. There are other parameter values that are passed in, there are other corner case handlings or whatever. Our purpose is to illustrate the higher level concept rather than to create a replacement for these methods. Exclusion is a very common thing. So let's say you start out with a list that's produced by some, you are a consumer of some list or somebody passes you a list.

[00:04:33] The first thing you might wanna do is transform all the values, so might call a map on it. And then you might wanna say after that transformation there's some characteristic of those things where some of those are not ones I want anymore. So, you'd call .map, and you'd chain off of that and call .filter, and you'd end up with an array that potentially had fewer items in it.

[00:04:53] So, that's how we can compose those two operations together. That's one way of composing. Of course you could probably think about ways to compose map and filter together that's much more complex than composing two maps together. But it's not impossible to do. Questions about exclusion. Did we have a question about transformation in the?

[00:05:22]
>> Speaker 2: It was kind of just a more general one. He's basically asking are map filter reduce some every for each the common functional approaches to problems? Or are they notions that come from a different, more functional domain?
>> Kyle Simpson: Not sure I understand the question. Our map filter reduce [INAUDIBLE].

[00:05:49] Common functional approaches to problems. What's confusing me about this question is that it seems to be asking the same thing in both clauses. Are they common functional or do they come from a functional domain? That seems to be the same thing to me. So I'm not sure I can directly answer the question-

[00:06:10]
>> Speaker 2: They're a few seconds behind here so you might add to this.
>> Kyle Simpson: Let me just speak to it more generally, if we're waiting for another question. Some of those in that list are very functional. Some of them are more JavaScripty. Map and we're about to talk about reduce, very functional.

[00:06:31] You'll find that in virtually all functional implementations. Filter, pretty common sometimes filter isn't added and you can just make filter as a, filter can be implemented based upon reduce which is what we're gonna talk about in just a minute. So, some implementations will only provide you the two perimatives of map and produce and then you build other high level perimatives on top of that.

[00:06:56]
>> Speaker 2: So yeah added to that there.
>> Kyle Simpson: They do absolutely come from functional programming techniques. The question, they come from functional programming or are they just convenience methods? The implementation of them as chained prototype methods is about convenience. But the way that they do the thing they do, that is very, very functional.

[00:07:18] That's an adaptation of that functional mindset into a JavaScript idiom.