Learning object 21 of 100
Filter selection visualizer
See which items filter keeps and which it drops, with the value your callback returned for each one.
Loading…
What it shows
filter keeps an item when the callback returns a truthy value and drops it otherwise. The kept items stay in their original order in a new array; the original is not changed. A callback that returns a non-boolean (like a string or a number) is treated as truthy or falsy.
Guided experiment
- Change the limit from 20 to 10. Predict which items are kept first.
- Use
(p) => p % 2on whole numbers. Why does this keep the odd ones? - Try a callback that returns a string. What happens?
Predict
What does [0, 1, 2, "", "a"].filter((x) => x) return?
Related
Lessons: Lesson 33: filter and selection
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-021.js in the companion repository.