Imagine a sieve at the Gem Vault. You pour in ALL the gems, and only the ones that match your criteria fall through. That's .filter()!
.filter() tests each item in an array and keeps only the ones that pass. It's perfect for finding:
How it works:
• g => g >= 50 is a test function — it returns true (keep) or false (discard)
• Items that return true stay in the new array
• The original array stays unchanged!
Remember: In Crystal Cavern, you used .sort() to ORGANIZE items. Now with .filter(), you can SELECT only the items you need!
Can you filter words that have more than 4 letters? Use .length in your filter!
Filter to keep only even numbers using the modulo operator % 2 === 0
First filter gems over 50, then use .map() to double their values!
Filter an array of gem objects by their rarity property!
Filter gems over 40, then use .reduce() to find the total value!
.filter() — Keeps only items that pass your testtrue to keep, false to discard.map(), .sort(), and .reduce()