[英]Vue JS filter by multiple array object items
I have some code which filters an array of objects based on a search input, it filters based on the search
variable. 我有一些代码可以根据搜索输入过滤对象数组,并根据
search
变量进行过滤。 I'd like to be able to search based on this, and potentially other keys in my object: 我希望能够基于此以及对象中的其他键进行搜索:
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())
})
}
}
}
Above is my code, which filters the title
key based on the search
input, however, each object contains title
, tags
and body
, tags
is an array, and body
is a string. 上面是我的代码,该代码根据
search
输入过滤title
键,但是,每个对象都包含title
, tags
和body
, tags
是一个数组,而body
是一个字符串。
How would I go about doing this? 我将如何去做呢?
Use ||
使用
||
operator to match other keys 运算符以匹配其他键
filteredGuides: function() {
return this.guides.filter(guide => {
return guide.title.toLowerCase().includes(this.search.toLowerCase())
|| guide.body.toLowerCase().includes(this.search.toLowerCase())
})
}
Dealing with different types depends on object strcture. 处理不同类型取决于对象结构。 Also, remeber you can extract
||
另外,记住您可以提取
||
clauses in another function. 另一个功能中的子句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.