Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed JavaScript Array Iteration Methods!
You have completed JavaScript Array Iteration Methods!
Preview
The filter method can be used to remove elements from an array.
Snippets from the Video
const names = ['Selma', 'Ted', 'Mike', 'Sam', 'Sharon', 'Marvin'];
let sNames = [];
names.forEach(name => {
if(name.charAt(0) === 'S') {
sNames.push(name);
}
});
console.log(sNames);
const numbers = [1,2,3,4,5,6,7,8,9,10];
// Result: [2,4,6,8,10]
MDN Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
you learned how to use forEach
to iterate over an array.
0:00
The rest of the methods you'll learn in
this course follow a similar pattern, but
0:01
they accomplish different things.
0:05
Let's start with the filter method.
0:07
You can use filter to
remove items from an array.
0:09
Filter doesn't affect the original array.
0:13
Instead, like other methods you'll learn
about here, it returns a newArray.
0:16
In this case, an array without
the items you wanted removed.
0:21
Say for example that you have
an array of ages as numbers, and
0:25
you want to remove every age below 18 and
over 24.
0:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up