Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在filter方法中返回与&&内部回调函数连接的布尔值?_Javascript_Loops_Filter_Callback_Higher Order Functions - Fatal编程技术网

Javascript 如何在filter方法中返回与&&内部回调函数连接的布尔值?

Javascript 如何在filter方法中返回与&&内部回调函数连接的布尔值?,javascript,loops,filter,callback,higher-order-functions,Javascript,Loops,Filter,Callback,Higher Order Functions,我正在寻找一种优雅的方法来生成布尔函数,最终在filter方法中的回调函数中使用&&运算符将布尔函数连接起来 我尝试循环过滤条件,但找不到将每个迭代结果合并为以下格式的方法: return Boolean && Boolean && Boolean && Boolean && Boolean 因为+=&&Boolean不起作用 以下是我所拥有的和正在发挥作用的: //data I am filtering this.preSear

我正在寻找一种优雅的方法来生成布尔函数,最终在filter方法中的回调函数中使用&&运算符将布尔函数连接起来

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

return Boolean && 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
})

我需要可扩展且更加优雅的解决方案,这样我就不必再添加一行&&如果我增强了我的过滤阵列。

您可以通过如下方式进行应用:

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);
    });
});

您可以通过如下方式进行申请:

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);
    });
});
您可以将其用于过滤器阵列

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

var preSearch=[[指环王,J.R.托尔金,英国,1955年,1.5亿欧元],[小王子,安托万·德·圣埃克苏佩里,法国,1943年,1.4亿欧元],[哈利·波特与魔法石,J.K.罗琳,英国,1997年,1.2亿欧元],[霍比特人,J.R.托尔金,英国,1937年,1亿欧元],[然后就没有了,阿加莎·克里斯蒂,英国,1939年,1亿欧元],[红楼梦,曹雪芹,中国,1791年,1亿欧元], 过滤器=[,英语,19,1].maps=>s.toLowerCase, 结果=搜索前 .filterrow=>filters.everyv,i=>row[i].toLowerCase.includesv; console.logresult; .as控制台包装{max height:100%!important;top:0;}可用于筛选器数组

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

var preSearch=[[指环王,J.R.托尔金,英国,1955年,1.5亿欧元],[小王子,安托万·德·圣埃克苏佩里,法国,1943年,1.4亿欧元],[哈利·波特与魔法石,J.K.罗琳,英国,1997年,1.2亿欧元],[霍比特人,J.R.托尔金,英国,1937年,1亿欧元],[然后就没有了,阿加莎·克里斯蒂,英国,1939年,1亿欧元],[红楼梦,曹雪芹,中国,1791年,1亿欧元], 过滤器=[,英语,19,1].maps=>s.toLowerCase, 结果=搜索前 .filterrow=>filters.everyv,i=>row[i].toLowerCase.includesv; console.logresult; .作为控制台包装{最大高度:100%!重要;顶部:0;}