javascript - 如何将 bool 函数数组组合成单个 bool 函数?

标签 javascript arrays function

我有许多可变长度的函数数组,其中数组中的每个函数都采用相同类型的单个参数并返回 true 或 false。

如何将这些函数“组合”成一个函数来测试每个组件函数是否返回 true?

var less_than = function(y) {
    function(x) {
    return x < y;
  }
}

var greater_than = function(y) {
    function(x) {
    return x > y;
  }
}

var is_even = function(x) {
    return x % 2 == 0;
}

var fns = [less_than(10), greater_than(1), is_even];

var test_function = combine_tests(fns);
test_function(8) // => true

最佳答案

The every() method tests whether all elements in the array pass the test implemented by the provided function.

var functions = [f1, f2, f3];
var arg = "foo";
var result = functions.every(function(func){
    return func(arg);
});

关于javascript - 如何将 bool 函数数组组合成单个 bool 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35219469/

相关文章:

c - 为什么我的 C 语言 GCD 程序没有运行?

javascript - JavaScript 中的函数行为不一致

javascript - 变量读数中的日期为 "Invalid Date"

javascript - 在 UIWebView 中将脚本作为文本加载

javascript - 选择具有特定 css 类的第一个元素

postgresql - 如何启用 PostgreSQL 函数分析器?

javascript - 如何在解决多个功能后才运行一个功能?

arrays - 数组未从 For Loop VBA 填充

javascript - 如何在JS中删除数组中的Vector

python - Fortran 有序(列优先)numpy 结构化数组可能吗?