Filter

👨‍💼 The filter method creates a new array with only elements that pass a test.
const numbers = [1, 2, 3, 4, 5]
const evens = numbers.filter((n) => n % 2 === 0)
// [2, 4]
The callback returns true to keep the element or false to exclude it.
🐨 Open and:
  1. Filter products by category
  2. Filter products by price range
  3. Chain filter with map
💰 Filter syntax:
const result = array.filter((element) => condition)
💰 Chaining filter and map:
const expensiveNames = products.filter((p) => p.price > 100).map((p) => p.name)

Please set the playground first

Loading "Filter"
Loading "Filter"