给定:
const a = () => true
const b = () => false
const t = [a, b]
我如何生成添加剂并:
a() && b()
在这种情况下,它将返回 false
。
最佳答案
您可以通过使用 true
作为 startValue 来减少数组并执行 logical AND &&
通过使用函数调用的结果。
这种方法保留函数调用的值。
const a = () => true
const b = () => false
const t = [a, b]
console.log(t.reduce((r, f) => r && f(), true));
使用结果存储值和短路的方法。
const AND = array => {
var result = true;
array.every(fn => result = result && fn());
return result;
};
关于javascript - AND 函数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59202279/