简体   繁体   中英

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)
      });
    }
  }

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)
    });
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM