【问题标题】:Vue array RegExpVue 数组正则表达式
【发布时间】:2020-07-28 13:58:09
【问题描述】:

我需要过滤带有复选框的列表。

这是如何从 VUEX 循环数组

<div class="checkbox" v-for="brand in brands" :key="brand.id">
   <input name="brands" type="checkbox" :value="brand.name" v-model="checkedBrand" />
   <label for="brands">{{brand.name}}</label>
</div>

这是我的功能

filteredList() {
      if (this.checkedBrand.length > 0) {
        return this.shoes.filter(shoe => {
          return shoe.brand.match(
            new RegExp(
              this.checkedBrand.forEach(check => {
                return +check + "|";
              }),
              "g"
            )
          );
        });
      } else {
        return this.shoes;
      }
    }

当它是新的 RegExp(checkedBrand[0]+'|'+checkedBrand[1], 'g'),但我不想被硬编码。

【问题讨论】:

    标签: javascript regex vue.js filter match


    【解决方案1】:
    filteredList() {
      if (this.checkedBrand.length > 0) {
        return this.shoes.filter(shoe => {
          return shoe.brand.match(
            new RegExp(
              this.checkedBrand.join('|'),
              "g"
            )
          );
        });
      } else {
        return this.shoes;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 2020-09-25
      • 2017-04-23
      • 1970-01-01
      相关资源
      最近更新 更多