我必须考虑“and”的大量参数,然后对模板执行操作。
所以,我在客户端创建了下面的助手。
Template.registerHelper('isIdle', function (...arg) {
// how to loop and do "AND" operation with all arugments here.
});
从 UI,我可以传递任意数量的参数,如下所示
{{isIdle isOnline isWorking isMoving isUsingChrome}}
如何循环“n”个参数并执行AND操作?我想检查的只是(isOnline && isWorking && .......)
等等
您可以使用reduce
:
function and(...arg) {
return arg.reduce( (res, bool) => res && bool );
}
// Example calls:
console.log(and(true)); // true
console.log(and(false)); // false
console.log(and(true, true)); // true
console.log(and(true, false)); // false
如果您想接受没有参数传递给此函数的情况,并希望该函数将其解释为空洞的真相,则可以使用 的第二个参数reduce
:
function and(...arg) {
return arg.reduce( (res, bool) => res && bool, true );
}
console.log(and()); // true
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句