如果数组中有虚假值,则返回

时间:2017-12-14 21:46:31

标签: javascript arrays

我想要发生的是,如果任何值是假的,只返回错误,永远不会返回万岁。我正在使用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');
    }
});

2 个答案:

答案 0 :(得分:3)

由于您使用的是非核心,请检查Collections.every/all

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

答案 1 :(得分: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);