【问题标题】:How to return booleans joined with && inside callback function in filter method?如何在过滤器方法中返回与 && 内部回调函数连接的布尔值?
【发布时间】:2019-03-27 21:07:01
【问题描述】:

我正在寻找一种优雅的方式来生成布尔值,这些布尔值最终将在过滤器方法的回调函数中使用 && 运算符进行连接。

我尝试循环遍历过滤条件,但找不到将每个迭代结果加入以下格式的方法:

return Boolean && Boolean && Boolean && Boolean && Boolean

因为 += && 布尔值不起作用。

这是我所拥有的和正在工作的:

//data I am filtering
this.preSearch = [
  ["The Lord of the Rings", "J. R. R. Tolkien", "English", "1955", "150 milionów"],
  ["Le Petit Prince (The Little Prince)", "Antoine de Saint-Exupéry", "French", "1943", "140 milionów"],
  ["Harry Potter and the Philosopher's Stone", "J. K. Rowling", "English",  "1997", "120 milionów"],
  ["The Hobbit", "J. R. R. Tolkien", "English", "1937", "100 milionów"],
  ["And Then There Were None", "Agatha Christie",   "English", "1939",  "100 milionów"],
  ["Dream of the Red Chamber",  "Cao Xueqin",   "Chinese", "1791", "100 milionów"]
]

//filters, that are set dynamically but let's pretend they are equal to
var filters = ["", "", "english", "19", "1"]

var searchdata = this.preSearch.filter(row => {
          return 
    row[0].toLowerCase().indexOf(filters[0].toLowerCase()) > -1 
    && row[1].toLowerCase().indexOf(filters[1].toLowerCase()) > -1 
    && row[2].toLowerCase().indexOf(filters[2].toLowerCase()) > -1 
    && row[3].toLowerCase().indexOf(filters[3].toLowerCase()) > -1 
    && row[4].toLowerCase().indexOf(filters[4].toLowerCase()) > -1
})

我需要可扩展且更优雅的解决方案,因此如果我增强过滤后的数组,我就不必再用 && 添加一行。

【问题讨论】:

    标签: javascript loops filter callback higher-order-functions


    【解决方案1】:

    您可以将Array#every 用于过滤器数组。

    为了更快地检查,您可以提前将过滤器值转换为小写。

    var preSearch = [["The Lord of the Rings", "J. R. R. Tolkien", "English", "1955", "150 milionów"], ["Le Petit Prince (The Little Prince)", "Antoine de Saint-Exupéry", "French", "1943", "140 milionów"], ["Harry Potter and the Philosopher's Stone", "J. K. Rowling", "English", "1997", "120 milionów"], ["The Hobbit", "J. R. R. Tolkien", "English", "1937", "100 milionów"], ["And Then There Were None", "Agatha Christie", "English", "1939", "100 milionów"], ["Dream of the Red Chamber", "Cao Xueqin", "Chinese", "1791", "100 milionów"]],
        filters = ["", "", "english", "19", "1"].map(s => s.toLowerCase()),
        result = preSearch
            .filter(row => filters.every((v, i) => row[i].toLowerCase().includes(v)));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案2】:

      您可以像这样应用Array.every()String.includes() 来做到这一点:

      var searchdata = this.preSearch.filter(row => {
      
          // this only returns true if our condition works for
          // index = 0, 1, 2, 3, 4
          return [0, 1, 2, 3, 4].every(index => {
              const rowContent = row[index].toLowerCase();
              const filterContent = filters[index].toLowerCase();
      
              // String.includes() is nicer than String.indexOf() here because
              // you don't need the ugly -1
              return rowContent.includes(filterContent);
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2021-11-27
        • 1970-01-01
        • 1970-01-01
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        • 1970-01-01
        • 2012-11-03
        相关资源
        最近更新 更多