2017-12-14 9 views
0

何か起こりたいのは、値が偽であれば、エラーだけを返すことで、それを返すことはありません。私はlodashを使用しています。 undescoreを使用して、あなたのしているので偽の値が配列に含まれている場合に返します

var jawn = [ 
    { 
     "cheese" : true, 
     "with" : true, 
     "without" : true 
    }, 
    { 
     "cheese" : true, 
     "with" : true, 
     "without" : true 
    }, 
    { 
     "cheese" : true, 
     "with" : false, 
     "without" : true 
    }, 
    { 
     "cheese" : true, 
     "with" : true, 
     "without" : true 
    }  
]; 

_.forEach(jawn, function (item) { 
    if(item.with === false) { 
     console.log('error'); 
    } else { 
     console.log('hooray'); 
    } 
}); 
+2

あなただけfalsyチェックをしたい場合は、 '(item.with!)'べきか作業?あなたが何を求めているのかは分かりません。 "チーズ"、 "あり"、または "なし"のいずれかが偽であればエラーを返しますか? – CRice

+5

'jawn.some(o =>!o.with)' –

+1

[次へ](https://stackoverflow.com/questions/23604734/how-to-check-if-all-object-keys-has-偽値) – oneturkmen

答えて

3

Collections.every/allを確認してください。

const msg = _(jawn).all(obj => obj.with) ? "hooray" : "error"; 
console.log(msg); 
+1

説明を追加するとすばらしいことになります – sumit

3

var jawn = [{ 
 
    "cheese": true, 
 
    "with": true, 
 
    "without": true 
 
    }, { 
 
    "cheese": true, 
 
    "with": true, 
 
    "without": true 
 
    }, { 
 
    "cheese": true, 
 
    "with": false, 
 
    "without": true 
 
    }, 
 
    { 
 
    "cheese": true, 
 
    "with": true, 
 
    "without": true 
 
    } 
 
]; 
 

 
const msg = jawn.some(item => !item.with) ? "error" : "hooray"; 
 
console.log(msg);

関連する問題