Reputation: 2945
I'm trying to do an if / else statement inside a computed property for Vue JS for a search, this is what I've got and it's not working, how could I adapt this to work?
computed: {
filteredProperties: function(){
return this.properties.filter((property) => {
return property.address.match(this.searchAddress) &&
if (this.searchType.length > 1) {
this.searchType.some(function(val){
return property.type.match(val)
}) &&
} else {
property.type.match(this.searchType) &&
}
property.bedrooms.match(this.searchBedrooms) &&
property.county.match(this.searchCounty)
});
}
}
Upvotes: 4
Views: 14839
Reputation: 25211
Your syntax is wrong, can't use an if statement in the middle of an expression. This would work:
computed: {
filteredProperties: function(){
return this.properties.filter((property) => {
let searchTypeMatch = this.searchType.length > 1
? this.searchType.some(function(val){
return property.type.match(val)
})
: property.type.match(this.searchType)
return property.address.match(this.searchAddress) &&
searchTypeMatch &&
property.bedrooms.match(this.searchBedrooms) &&
property.county.match(this.searchCounty)
});
}
}
Upvotes: 1