Javascript
JavaScript Fundamentals -- Arrays in JavaScript

Array.filter

PRO
Outline

The filter method on arrays is meant for filtering out some items from a starting array based on a conditional statement that you provide. If the conditional statement resolves to truthy, the item is included in the resulting array; if it resolves to falsy then the item is not included in the resulting array. Here's an example:

const scores = [6, 7, 8 , 9];
const filtered = scores.filter(score => score > 7);
console.log(filtered); // [8, 9]

In this example, we have an array of scores. The four items are 6, 7, 8, and 9. The filter method condition says that if the item in the array is greater than 7, it should be included in the resulting array. Only 8 and 9 are greater than 7, so our resulting array has a length of 2.

Just like the map method, this method does not alter the original array.

 

I finished! On to the next chapter