In the last few episodes, you've seen how you can work with higher order functions to perform actions on items without modifying arrays. Take a look at one of those functions called filter to see how we can take care of this common task.
- [Instructor] Now searching through a series of elements can be accomplished in different ways. In the last few episodes, I've been showing you how you can work with higher order functions to perform actions on the items with a modifying array. Now this is actually a style of programming called functional programming, and one of the other functions that we can use to work with this is called the filter function, so let's take a look at how that work, because it lets us solve a pretty common task. So I'm assuming you have some data that looks like this, and we want to filter through this data and retrieve only the records that match a certain element.
Now that type of manipulation is called exclusion, because we're removing elements that we want to filter out of a list. So let's take a look at how we might do that with traditional for loops. Here's what we would probably do, we would create a variable to hold the thing that we want to search for, in this case the word rabbit, and then we would have to create an array to push our results into. Now here's a typical for loop that goes through the arrays and although this isn't technically modifying the elements in the array, it's going through each of the elements and assigning each individual one of these to an item variable, and then what we want to do is go through each one of the array methods and then test to see if the element that we are looking for is found in a specific node of our array.
So this myArr[i].apNotes, if you remember from the previous slide, is looking for in the array, for anything with the appointment notes. So we're testing to see if the word rabbit exists right here, and of course, we're going to get this record in the array that we're looping through. So let's go back to this, if we find that a record matches, then it's going to go ahead and push it into our new array. This is rather confusing, as you can tell just from reading this, there's a lot of code, and I don't think it's as clear as the functional style, and what we're doing here is actually printing out the result array.
So let's take a look at how we would do this with functional programming, and using the filter function, we just consider it to be a higher order function. Here's our same result using our filter method, so you can see that I'm doin' it right here, and you can see that we can assign the results of this filter, directly into a variable. So we didn't have to create a separate array, and then sort of push things into that array, because that is what filter is designed to do, go over a list, and then modify the list if what happens in our filter turns out to be true.
So here is what we're doing, we're using arrow functions, which I know are a little bit more confusing, but this is the same thing. We're creating a function, passing it along each of the items, so that would be each of the elements in our list, and so the first time as it is going through and filtering things, it would get this object, and then the next time, it would get this object. Let's go back here. And as it's going through that, it's going to perform a test.
So whenever you see the arrow, you can replace that with the curly braces, and we want to do just one thing. So we could actually write all this out in a single line, and this is the way that a lot of people like to see this, but it doesn't quite fit on this line, so I'm just putting some sort of carriage return right there. What we're doing is we're passing each of the items and then we are looking in the appointment notes element of each of the list items, to see if, just like before, it matches the search text.
Now I'm doing one more thing here, and it's actually pretty important when you're searching. I'm converting each of the elements, the item appointment notes, and the search term to lower case, and that's because I want it to find the instance of for where the F is capitalized or where the F is lowercase. So this is just a little bit of a convenience, but notice that we can do that, and then we can use this match method, which looks for some text, and compares it to some other text.
So this right here is going to return a true statement if the element that we're testing for matches our test right here. Or it's going to exclude the element if it doesn't, and that's exactly what filter is designed to do, to exclude elements and not push them into an array if they don't match. So that is exactly what we need, and although a lot of times I say that you can solve problems in different ways and there's no good or bad way, when you get a question that's related to functional programming, people want to see you solve the problem in this way because they know that something like filter can be added to a series of different composing statements.
And as usual, if you have some ideas for this weekly series, and maybe you want to share with me some questions you've been asked, or have asked in interviews, connect with me in LinkedIn, or just about any other social media network, like Twitter or GitHub @planetoftheweb.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.