我有一个非常基本的数组:
specialityTest: [{person:'nurse', text:'text1'},
{person:'nurse', text:'text1'},
{person:'physician', text:'text1'},
{person:'physician', text:'text1'}]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用基本的过滤器功能对此进行过滤:
this.specialityTest.filter((person) => {
return (person =="physician")
})
Run Code Online (Sandbox Code Playgroud)
然后复制过滤的数组:
methods: {
seedSpeciality(){
this.nurseListSpeciality2 = JSON.parse(JSON.stringify(this.specialityTest.filter(
(person) => {
return (person =="physician")
})))
},
}
Run Code Online (Sandbox Code Playgroud)
这对我不起作用...我有错误:
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))
},
Run Code Online (Sandbox Code Playgroud)
对我来说它工作正常。
<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)
}
}
})
Run Code Online (Sandbox Code Playgroud)
您正在使用过滤器功能遍历对象数组,因此在每个循环中都会得到对象。因此,如果使用el.person ==“ physician”,它将很好地工作
| 归档时间: |
|
| 查看次数: |
4854 次 |
| 最近记录: |