javascript - 多条件过滤

标签 javascript vue.js

我正在使用多个复选框在 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/

相关文章:

javascript - 获取在嵌套循环中动态生成的选定选项值

javascript - 鼠标悬停时隐藏 div

javascript - 向 JS 类添加一个引导类

javascript - vue webpack-simple 模板和 Uncaught TypeError : (intermediate value) is not a function

javascript - 根据数组中的对象属性生成带头的列表

javascript - 我想动态创建 Vue 组件的新实例,并附加每个实例的信息。我的代码有什么问题吗?

javascript - 如何在 d3 图表的 x 轴上绘制左右矩形 slider

javascript - 消耗 angular.js 剩余请愿书的所有记录

vue.js - 从 Quasar 启动文件访问存储

vue.js - Vue 中观察者回调读取 null 属性时出错