最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue JS ifelse statment inside computed property - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 1

Your 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)
    });
  }
}
发布评论

评论列表(0)

  1. 暂无评论