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

I'm trying to do an ifelse statement inside a puted property for Vue JS for a search, this is w

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745306326a4621731.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信