Filtering arrays of objects

The filter method allows us to create a new array, containing only selected elements that are filtered by a certain condition. Array.filter() takes a callback parameter and returns an array, containing all values that were true according to our given condition.

Syntax

const pizzas = [
  { name: 'Extra hot', ingredients: ['Pepperoni', 'Cheese'] },
  { name: 'Hawaii', ingredients: ['Pineapple', 'Cheese'] },
  { name: 'Medium hot', ingredients: ['Pepperoni', 'Cheese'] },
];

const hotPizzas = pizzas.filter(pizza => pizza.ingredients.includes('Pepperoni'));
JavaScript