我有一个非常基本的数组:
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",它将正常工作
关于javascript - vuejs filter is not a function 报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803342/