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

In this exercise, students write a filter function to looks through each value in a list.

Get Unlimited Access Now

Transcript from the ".filter() Exercise" Lesson

[00:00:00]
>> So we're gonna start off with a quick recap, on where we are in our investigation so far. Really, we've just gotten started, but we've learned that things aren't always as they seem. War is an example of things being a little bit different than we expected.
>> Arrays and objects.

[00:00:21]
>> Yeah, arrays and objects. That was a big one. Anything else that comes to mind? No, that was the best, that's the one I was asking for, cool. So, our detective team, we're here working together. We have a lot of skills in working with, and manipulating data. And we're just getting better and better by the minute, really.

[00:00:44] And our team of elves who are secretly helping us out, were really hard at work last night. In just in, we have some data that they scraped from some video footage at the mansion where Mr. Body is thought to have been murdered. And we have this object of data, luckily our our data scrapers are also JavaScript programmers, we only associate with people who program in JavaScript.

[00:01:17] So, we choose our team wisely. So, we have this data to get us started. And now let's just kick off with a quick exercise where we're gonna filter through this data, and we wanna figure out who was present in this video footage at the mansion, the night of the murder.

[00:01:38] So, what is filter? Has anyone used filter before? And underscore or low dash? Couple people. So, filter is a function that takes an array and a callback, and it's going to return a new array, right? This is similar to map is gonna return a new array that will contain, this is the important part of filters that it will only contain the values that return true from the callback.

[00:02:07] So, the callback has to return a Boolean either true or false. If that callback returns true, then we're gonna save it to the array, If not, we won't. So with map, when we loop through all of the items in our list, we returned an array that was at the same length, right?

[00:02:27] Every time, with filter that's not always true, filter can be shorter than the original array, really depends on our callback function. And if it's returning true or false, we also call like this pretty good kind of callback function like a truth test, yeah.