Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 找到更好的方法查看对象是否已填充字段_Javascript_Vue.js_Vuejs2 - Fatal编程技术网

Javascript 找到更好的方法查看对象是否已填充字段

Javascript 找到更好的方法查看对象是否已填充字段,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,我用Vue安装了过滤器 模板: <b-button v-if="filterFilled()" @click="clearFilter"> Clear </b-button> 我检查过滤器是否脏的方法: filterFilled(){ return (this.filter.price_from || this.filter.price_to || this.filter.surface_to || this.filter.surface_fro

我用Vue安装了过滤器

模板:

<b-button v-if="filterFilled()" @click="clearFilter">
    Clear
</b-button>
我检查过滤器是否脏的方法:

filterFilled(){
        return (this.filter.price_from || this.filter.price_to || this.filter.surface_to || this.filter.surface_from ||
          this.filter.floor || this.filter.type || this.filter.structure)
      },

这很好,但我的问题是,是否存在询问对象是否已填充道具的更好方法?

您可以使用
检查此过滤器是否至少有一个值


您可以使用以下命令检查
此.filter
是否至少有一个值


除了@adiga suggestion之外,对于检查组件中数据的方法,您通常应该优先选择方法,因为它们的性能更高,并且只有在其某些依赖项发生更改时才会调用:

...

data () {
      return {
        filter: {
          price_from: '',
          price_to: '',
          surface_from: '',
          surface_to: '',
          floor: '',
          type: '',
          structure: '',
        },
      }
    },
  computed: {
     filterFilled() {
       // as in @adiga answer, which is great
       return Object.values(this.filter).some(v => v);
     }
  }

除了@adiga suggestion之外,对于检查组件中数据的方法,您通常应该优先选择方法,因为它们的性能更高,并且只有在其某些依赖项发生更改时才会调用:

...

data () {
      return {
        filter: {
          price_from: '',
          price_to: '',
          surface_from: '',
          surface_to: '',
          floor: '',
          type: '',
          structure: '',
        },
      }
    },
  computed: {
     filterFilled() {
       // as in @adiga answer, which is great
       return Object.values(this.filter).some(v => v);
     }
  }
...

data () {
      return {
        filter: {
          price_from: '',
          price_to: '',
          surface_from: '',
          surface_to: '',
          floor: '',
          type: '',
          structure: '',
        },
      }
    },
  computed: {
     filterFilled() {
       // as in @adiga answer, which is great
       return Object.values(this.filter).some(v => v);
     }
  }