javascript - vuejs filter is not a function 报错

标签 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")
      })))
  },
 }

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

未捕获( promise )TypeError:this.specialityTest.filter 不是函数 在 VueComponent.seedSpeciality (AddOfferDialog.vue?f4fc:1771) 在 VueComponent.boundFn [作为 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))
  },

最佳答案

For me it works fine.Take a look:

<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

关于javascript - vuejs filter is not a function 报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803342/

相关文章:

javascript - 如何处理 Phonegap + JQM 应用程序中的链接?

javascript - 使用 JavaScript 更改网页中的文本

javascript - 将应用程序部署到 Heroku,在后端 Java Spring Boot 中抛出 500 内部服务器错误,同时在本地环境中完美运行

javascript - 如何在 vue 组件中使用 Maatwebsite 导入 excel 文件?

javascript - VueJS - 绑定(bind)自定义 Prop 不适用于 b-form 组件

javascript - TypeScript Disallow 不必要的可选链表达式

javascript - 解析 JSON 返回神秘错误

javascript - IE8 控制台.log

vue.js - Vue 组合 API 计算属性测试

javascript - 数据变量作为 __ob__ 返回