'Vue JS if/else statment inside computed property
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)
});
}
}
Solution 1:[1]
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)
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeff |
