.filter()Concept: Refactoring the filter pattern with array methods
In Kata 7 we looped through numbers to pick out the evens. Let’s rewrite the same behavior using the .filter() helper.
Create a FilterEvensArrayMethod component that:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].filter() to keep only even numbersEven numbers: 2, 4, 6, 8, 10export default function FilterEvensArrayMethod() {
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Use .filter() here
return (
<div>
{/* Display the even numbers */}
</div>
);
}
.filter() keeps items that make the callback return true% 2 === 0 checks if a number is even.join(', ') to format the string.filter() in a descriptive variable like evenNumbersEven numbers: {evenNumbers.join(', ')}
.filter() returns a new array with only the items that match the conditiontrue to keep an item, false to skip it.filter()