2017-05-30 8 views
2

"と"のための膨大な数のパラメータを考慮して、テンプレートに対して操作を実行する必要があります。メテオでes6関数の "Anding" n引数

私はクライアント側でヘルパーの下で作成しました。 UIから

Template.registerHelper('isIdle', function (...arg) { 
    // how to loop and do "AND" operation with all arugments here. 
}); 

、私は

{{isIdle isOnline isWorking isMoving isUsingChrome}} 

どのように「n」の引数をループにしてやると操作以下のように任意の数の引数を渡すことができますか?私がチェックしたいすべてが(isOnline && isWorking && .......)あるので

+0

すべてのisXXXはブール値ですか? – dfsq

+1

は、forEach()またはreduce()またはmap()関数を使用できます。 lodashの方法を参照してください。私はreduce()を使うことをお勧めします。 – Plankton

+0

@dfsq:それは問題ではありません。もしそれが 'undefined'または' null'なら 'false'を返します –

答えて

3

にあなたは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

をこれをvacuous truthと解釈する関数は、reduceの第2引数を使用できます。

function and(...arg) { 
 
    return arg.reduce((res, bool) => res && bool, true); 
 
} 
 

 
console.log(and()); // true

+0

私はほとんどそれをしましたが、あなたは答えを投稿しました..たくさんの@trincot –

+0

あなたを歓迎します;-) – trincot

+0

良いリソースのための任意の提案は、es6を学ぶために(テキスト、動画はありません:) –

0

メテオ実装に特有のものです。

Template.registerHelper('isIdle', function (...args) { 
    return args.reduce((previous, current) => previous && current); 
}); 
関連する問題