I'm trying to do an if / else statement inside a puted 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?
puted: {
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)
});
}
}
I'm trying to do an if / else statement inside a puted 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?
puted: {
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)
});
}
}
Share
Improve this question
edited Sep 25, 2018 at 19:40
Ryan H
asked Sep 25, 2018 at 19:33
Ryan HRyan H
2,9956 gold badges56 silver badges157 bronze badges
5
-
2
it's not working
in which way is it not working? – Thomas Junk Commented Sep 25, 2018 at 19:35 -
render function or template not defined in ponent: anonymous
it's giving me an error, if I take the if/else statement away and have either one of the two functions then it works. – Ryan H Commented Sep 25, 2018 at 19:36 -
3
Using the
&&
like you're using is invalid...&& if (condition) {} &&
is invalid.}) && } else {
- definitely not valid. – tymeJV Commented Sep 25, 2018 at 19:38 - @tymeJV How would I make it valid? – Ryan H Commented Sep 25, 2018 at 19:39
-
1
You could use a ternary, or you could assign the results of the
if
logic to a variable, then just check the variable. – tymeJV Commented Sep 25, 2018 at 19:40
1 Answer
Reset to default 1Your syntax is wrong, can't use an if statement in the middle of an expression. This would work:
puted: {
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)
});
}
}