I'm using Vue and trying to filter an array of results using a JS function composition.
My Vue computed values are like so, but can't get filteredByAll to accept a third option getByThing. Right now the filterByAll just filters on the category and keyword search.
computed: {
filteredByAll() {
return getByCategory(getByKeyword(this.list, this.keyword), this.category)
},
filteredByKeyword() {
return getByKeyword(this.list, this.keyword)
},
filteredByCategory() {
return getByCategory(this.list, this.category)
},
filteredByThing() {
return getByThing(this.list, this.thing)
}
}
My standard JS functions
function getByKeyword(list, keyword) {
const search = keyword.trim()
if (!search.length) return list
return list.filter(item => item.name.indexOf(search) > -1)
}
function getByCategory(list, category) {
if (!category) return list
return list.filter(item => item.category === category)
}
function getByThing(list, thing) {
if (!thing) return list
return list.filter(item => item.thing === thing)
}
I'm trying to wrap my head around the functional stuff but struggling from what I've read.