[英]Return if a falsy value is in an array
我想發生的是,如果任何值都是虛假的,則僅返回錯誤,而永不返回萬歲。 我正在使用lodash 。
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');
}
});
由於您使用的是undescore,請檢查Collections.every / all :
const msg = _(jawn).all(obj => obj.with) ? "hooray" : "error";
console.log(msg);
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.