[Vuejs]-ES6/JS: Search nested object for string, return true

1👍

You can do it using the .filter() function, iterating first through the recipes, and then through the ingredients, looking for matches like this

// Assumes allRecipes is your array of recipe objects, 
// and we are looking for "ice"

const regex = /ice/i   // Note the "i" makes it a case-insensitive search

// You can also create this with regex = new RegExp(“ice”,”i”)

const recipes = allRecipes.filter( recipe => {
  const ingredients = recipe.ingredients.filter(ing => ing.ingName.match(regex))
  return ingredients.length > 0  // True if any matches
})

//At this point `recipes` will contain the list of filtered recipes
👤Mikkel

Leave a comment