我正在使用多个复选框在 booksFilter 函数中对其进行过滤。
data() {
return {
books:[
{
name:"Book1",
price:2000
},
{
name:"Book2",
price:2000
},
{
name:"Book3",
price:2000
},
{
name:"Book1",
price:1000
}
],
filters:{
options:[]
}
}
},
computed:{
booksFilter(){
return this.books.filter(x => {
if(this.filters.options.length>0){
return this.filters.options.indexOf(0)!=-1 && x.price>1000
|| this.filters.options.indexOf(1)!=-1 && x.name == "Book1";
}
else{
return true;
}
})
}
https://codepen.io/Aksultan/pen/NWKeZGg
当使用两个复选框时,它应该只返回一本书,但现在它返回整个数据。
谢谢
最佳答案
关于:
booksFilter() {
return this.books.filter(x => {
if (this.filters.options.length> 0) {
boolean condition = true;
if (this.filters.options.indexOf(0)!=-1) {
conditon = conditon && x.price>1000
}
if (this.filters.options.indexOf(1)!=-1) {
conditon = conditon && x.name == "Book1";
}
return condition;
else {
return true;
}
})
}
希望它能解决您的问题。
关于javascript - 多条件过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58060339/