JavaScript Simplified · One concept, one day, one project · Day 24 of 32
Array Methods: filter() and find()
Picking out the items you want from a list: many with filter, one with find. You build a product search that narrows a catalogue.
By the end of today you can
- Keep only matching items with
filter - Get the first matching item with
find - Choose between
filterandfindfor a real need - Build a product search
Yesterday map changed every item. Today you select. filter keeps the items that pass a test (a list). find returns the first item that passes (a single thing). Together with map, these three cover most of the data work in a real app.
1. filter: keep the matches
Definition
filter goes through an array and keeps only the items for which your function returns true. It returns a new array of those matches. Your function answers "should I keep this one?"
filter keeps each number for which n > 10 is true. The result is a new, shorter array; the original is unchanged.2. find: the first match
Definition
find also tests each item, but it returns the first item that passes, not a list. Use it when you want one specific thing. If nothing matches, it returns undefined.
find returns the whole first matching object, Ada. This is an array of objects, the shape from Week 3, which is exactly how real data arrives.| Method | Returns | Use when |
|---|---|---|
map | a new array, same length | changing every item |
filter | a new array, matches only | keeping some items |
find | one item (or undefined) | getting the first match |
Today’s Mini Project
🔎 Product Search
A catalogue of products as an array of objects. Filter it by category and by price, and find a single product by name. This is a real search, in a few lines.
Add a filter for tech products under 10000 (combine two conditions with &&). Expected: only the Mouse, since the Keyboard is too expensive.
From the products, use find to get the first food item, and print its price.
Expected: "Rice 5kg" at 6500, the first product whose category is food.
You have finished the modern layer. map, filter and find turn a pile of data into exactly what you want to show: transformed, narrowed, or a single pick. This is the daily work of every app that lists anything. Next week your data starts coming from the internet.
Checkpoint
What is the difference between filter and find?
filter returns a new array of all items that pass the test. find returns only the first item that passes (or undefined if none do).
What does your function return to filter or find?
True or false: "does this item match?" filter keeps every true; find stops at the first true and returns that item.
How do map, filter and find work together?
filter narrows a list, map reshapes items (like pulling out names), and find grabs one specific item. Together they cover most data work.
Tick Before You Move On
- ☐ I can keep matching items with filter
- ☐ I can get the first match with find
- ☐ I can choose between filter and find
- ☐ I built the Product Search
Quick Recap
filter returns a new array of the items that pass your test. find returns the first item that passes, or undefined. Your function answers true or false for each item. map, filter and find together cover most everyday data work.
Next: Day 25, and the layer that scares beginners for no reason: asynchronous JavaScript. You start gently, with what "async" even means.