Filter
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 use the provided
products fixture:- Create
electronics: products wherecategory === 'Electronics'(names:Laptop,Headphones,Monitor) - Create
affordable: products whereprice < 100(names:Toaster,Blender) - Create
available: products whereinStock === true(length4) - Create
inStockElectronicsUnder500: the names of products that are in stock, in the Electronics category, and haveprice < 500β expected['Monitor'] - Export
electronics,affordable,available, andinStockElectronicsUnder500
π°
filter keeps items where your callback returns true.