如何检查布尔值在每个数组对象中是否为假?

时间:2018-12-30 05:01:22

标签: typescript

我有一个包含多个对象的数组:

quesListArray = [
                   {Position: 1, Mandatory: false}, 
                   {Position: 2, Mandatory: true}, 
                   {Position: 3, Mandatory: false}, 
                   ...
                   ...
                ]

我如何知道每个对象中的“必填”字段是否为假。如果所有内容都不正确,那么我需要显示一条消息。

任何帮助将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:2)

questListArray上将every与箭头功能结合使用(为简洁起见),如下所示:

areAllMandatoriesFalse() {
   if (this.quesListArray.every(item => !item.Mandatory)) {
     alert("All are false");
   }
   else {
      alert("Not all are false");
   }
}

DEMO

答案 1 :(得分:1)

使用“每一个”。 例如:

function isBelowThreshold(currentValue) {
    return currentValue < 40;
}

var array = [1, 30, 39, 29, 10, 13];

console.log(array.every(isBelowThreshold));
// expected output: true

我希望我的帮助有效effective

答案 2 :(得分:0)

尝试一下:

checkPropAreFalse() {
    let MandatoryFlag = true;
    for (let index = 0; index < this.quesListArray.length; index++) {
      const element = this.quesListArray[index];
      if (!element.Mandatory) {
        continue;
      } else {
        MandatoryFlag = false;
        break;
      }
    }
    return MandatoryFlag;
  }

从文件中调用方法:

const responce = this.checkPropAreFalse();
相关问题