this.my_object.filter不是函数

时间:2019-05-09 14:10:43

标签: vue.js

这是模板(为简单起见,我仅添加了一个按钮):

<button v-on:click="test()">Test</button>

脚本:

export default {
  name: 'App',
  data: function() {
    return {
      boss_type: {
        prisoner: 'Some value',
        guardian: 'Some value',
        recidivist: 'Some value',
        moscow: 'Some value',
        secret: 'Some value',
        hospital: 'Some value'
      }
    }
  },
  methods: {
    test: function() {
        this.boss_type.filter((type) => {
          console.log(type);
        });
    }
  }
...

点击时发生错误:[Vue warn]: Error in v-on handler: "TypeError: this.boss_type.filter is not a function"

请帮助!

1 个答案:

答案 0 :(得分:1)

.filter()是数组的方法,boss_type是对象。您可以使用Object.keys()从对象输入返回一个数组:

test: function() {
    Object.keys(this.boss_type).filter(key => {
      console.log("key: ", key);
      console.log("value: ", this.boss_type[key]);
    });
}
相关问题