Skip to content

Instantly share code, notes, and snippets.

@Thaekeh
Created December 2, 2020 12:58
Show Gist options
  • Save Thaekeh/11e3d60d1dd1a0074ba24de4a1aef0ed to your computer and use it in GitHub Desktop.
Save Thaekeh/11e3d60d1dd1a0074ba24de4a1aef0ed to your computer and use it in GitHub Desktop.
This is the complete sort, filter, and search code.
computed: {
filteredRecipes() {
let tempRecipes = this.recipes
if (this.searchValue != '' && this.searchValue) {
tempRecipes = tempRecipes.filter((item) => {
return item.title
.toUpperCase()
.includes(this.searchValue.toUpperCase())
})
}
if (this.maxCookingTime)
tempRecipes = tempRecipes.filter((item) => {
return (item.cookingTime <= this.maxCookingTime)
})
tempRecipes = tempRecipes.sort((a, b) => {
if (this.sortBy == 'alphabetically') {
let fa = a.title.toLowerCase(), fb = b.title.toLowerCase()
if (fa < fb) {
return -1
}
if (fa > fb) {
return 1
}
return 0
} else if (this.sortBy == 'cookingTime') {
return a.cookingTime - b.cookingTime
}
})
if (!this.ascending) {
tempRecipes.reverse()
}
return tempRecipes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment