我有一些代码可以根据搜索输入过滤对象数组,它根据 search
变量进行过滤。我希望能够根据此搜索,以及我的对象中可能的其他键:
import HelpGuides from '~/static/help/help-guide.json';
export default {
head: {
title: 'Help'
},
data () {
return {
guides: HelpGuides,
search: ''
}
},
computed: {
filteredGuides: function() {
return this.guides.filter(guide => {
return guide.title.toLowerCase().includes(this.search.toLowerCase())
})
}
}
}
上面是我的代码,它根据 search
输入过滤 title
键,但是,每个对象都包含 title
、 Tags
和 body
,tags
是一个数组,body
是一个字符串。
我该如何去做呢?
最佳答案
使用||
运算符来匹配其他键
filteredGuides: function() {
return this.guides.filter(guide => {
return guide.title.toLowerCase().includes(this.search.toLowerCase())
|| guide.body.toLowerCase().includes(this.search.toLowerCase())
})
}
处理不同类型取决于对象结构。另外,请记住您可以在另一个函数中提取 ||
子句。
关于javascript - Vue JS 按多个数组对象项进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57893227/