JavaScript · Level 2 Arrays

map & filter

Keep the items you want with filter and reshape each one with map, without ever touching the original array.

  • filter keeps items that pass a test
  • map transforms every item
  • Both return a new array
  • Arrow functions with braces need return

filter keeps the items that pass a test; map turns each item into something new. Both return a new array and leave the original alone. Trap: an arrow function with braces { } needs return.

const prices = [5, 12, 30];

prices.filter(p => p > 10);    // [12, 30]
prices.map(p => p * 2);        // [10, 24, 60]
prices.map(p => { p * 2; });   // [undefined, undefined, undefined]
prices;                        // [5, 12, 30] — unchanged

Write activeNames(users) that returns the names of the users whose active is true, for example [{ name: "Ada", active: true }, { name: "Bob", active: false }] gives ["Ada"].

solution.js
Test Suites Pending
Output