vuejs过滤器不是函数错误

时间:2018-04-12 18:11:23

标签: javascript vue.js

我有一个非常基本的阵列:

specialityTest: [{person:'nurse', text:'text1'}, 
{person:'nurse', text:'text1'}, 
{person:'physician', text:'text1'}, 
{person:'physician', text:'text1'}]

我尝试使用基本过滤功能对此进行过滤:

      this.specialityTest.filter((person) => {
        return (person =="physician")
      })

然后复制过滤后的数组:

  methods: {
   seedSpeciality(){
      this.nurseListSpeciality2 = JSON.parse(JSON.stringify(this.specialityTest.filter(
      (person) => {
        return (person =="physician")
      })))
  },
 }

这对我不起作用......我有错误:

Uncaught (in promise) TypeError: this.specialityTest.filter is not a function at VueComponent.seedSpeciality (AddOfferDialog.vue?f4fc:1771) at VueComponent.boundFn [as seedSpeciality]

我在这里做错了什么?

编辑: 我从api调用运行此函数:

  fetchSpecialityArray(){
    this.$http.get('http://127.0.0.1:8000/api/speciality')
      .then(response => response.json())
      .then(result => this.specialityTest = result).then(this.seedSpeciality.bind(this))
  },

1 个答案:

答案 0 :(得分:3)

  

对我来说它运作正常。看看:

<div id="app">
  <button @click="filter">Filter</button>
</div>

new Vue({
  el: "#app",
  data: {
    specialityTest: [
      {person:'nurse', text:'text1'}, 
      {person:'nurse', text:'text1'}, 
      {person:'physician', text:'text1'}, 
      {person:'physician', text:'text1'}
    ]
  },
  methods: {
    filter() {
        let filtered = this.specialityTest.filter((el) => {
        return (el.person =="physician")
      })
      console.log(filtered)
    }
  }
})

你正在使用过滤器函数循环遍历对象数组,所以在每个循环中你得到了对象。所以如果你使用el.person ==“physician”它会正常工作

看看my jsfiddle