How can I assist you today?
Oh, interesting! But could you also give me that `filter` example in JavaScript?
Of course! The filter
method lets you create a new array with only the elements that meet a certain condition. Here's a
quick example:
const numbers = [1, 2, 3, 4, 5, 6];
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4, 6]
This keeps only the even numbers from the original array.
That makes sense! When should I use `map` versus `filter`?
Great follow-up! Use map
when you want to transform all items in an array into something new. Use filter
when you want to remove some items based on a condition but keep the rest as they are.