Array.filter (again)
Keep only items that pass a test — with richer predicates and both callback styles.
Phase 10 introduced filter. Here we stress that the original
array is unchanged, show compound tests, and write callbacks as both a
function expression and an arrow.
Lengths: original 0 kept 0
Before
After (new array)
Important JavaScript
const kept = players.filter(function (player) {
const scoreOk = player.score >= minScore;
const categoryOk = category === "any" || player.category === category;
const activeOk = activeOnly ? player.active : true;
return scoreOk && categoryOk && activeOk;
});
// Same idea with an arrow
const keptArrow = players.filter((player) => {
return player.score >= minScore && player.active;
});
// Original length unchanged; kept may be shorter (even empty).