← Home BP Β· 7-9 πŸ’Ž

Treasure Filter

71
Day 1 πŸ”₯
🎯 Today's Missions
πŸ΄β€β˜ οΈ πŸ’° πŸ‘‘ πŸ—οΈ
πŸ’Ž
Treasure Island Universe
Treasure Filter
In Dragon Array you stored items in a list. Now learn .filter() to find only the treasures you want, and .map() to transform every item! filter(t => t.value > 100) keeps only valuable gems. map(t => t.gold) turns them all into gold! πŸ“Š

πŸ’Ž What we're MAKING today: My Treasure Filter

In Dragon Array you stored treasures in a list with push() and pop(). But what if you want to FIND only the expensive ones? Or turn every treasure into its gold value? That's where .filter() and .map() come in! πŸ’Ž

What is .filter()? It looks at every item in your array and KEEPS only the ones that pass a test. treasures.filter(t => t.value > 100) keeps only treasures worth more than 100 gold! What is .map()? It TRANSFORMS every item into something new. treasures.map(t => t.name) creates a list of just the names! Combined, these turn a messy pile into exactly what you need! ✨

πŸ”— Last time in Dragon Array you learned dragons.push() to add items and dragons.length to count them. Today, instead of just adding items, you'll FILTER to find specific items and MAP to transform them. Same array, superpowers! πŸ’Ž
treasure-vault.js JavaScript
var treasures = [
  { name: "Ruby", emoji: "❀️", value: 250 },
  { name: "Sapphire", emoji: "πŸ’Ž", value: 180 },
  { name: "Emerald", emoji: "πŸ’š", value: 320 },
  { name: "Gold Coin", emoji: "πŸͺ™", value: 50 },
  { name: "Diamond", emoji: "πŸ’ ", value: 500 }
];

// Use the SLIDER to set minValue
// Then press FILTER to call .filter()
πŸ’‘ Use the slider to set your gold threshold .filter() finds Β· .map() transforms
vault view LIVE
πŸ“Š Total items
5
✨ Filtered
5
πŸ’° Gold value
1,300
πŸ’Ž All Treasures (array)
✨ Filtered Result (.filter + .map)
Step 1 / 5
Filter by Gold! Set a minimum value with the slider, then press Filter. Try setting the slider to 150 gold β€” only treasures worth 150+ should appear!
πŸ’° Minimum Gold Value (filter threshold)
Min Value: 0
✨ Show .map() transformation
OFF β€” showing filtered names only
πŸ“‹ Your 5 Filter Challenges
0 of 5 challenges complete
πŸ’Ž
You Mastered Treasure Filter!
You learned .filter() to FIND only what you need, and .map() to TRANSFORM everything! These two methods turn a raw array into exactly what you want β€” the superpowers every real app uses! πŸ’Žβœ¨

πŸ’« What You Learned

  • .filter(fn) keeps only items that pass the test β€” arr.filter(x => x.value > 100)
  • The test function returns true (keep) or false (remove)
  • .map(fn) transforms every item β€” arr.map(x => x.name) gives you just names!
  • Chain them! arr.filter(...).map(...) filters THEN transforms!
  • Arrays power real apps: filtering search results, transforming data, converting currencies!
πŸ’Ž Your Creation
My Treasure Filter
πŸ’Ž Come Back Tomorrow!
Next up: Array Sorting β€” what if you want to sort treasures from cheapest to most expensive? Learn .sort() to order your array by any property! πŸ’°
🏠 Home
Nice! πŸŽ‰