Typescript 如何检查中数组的每个对象中的布尔值是否为false?
我有一个由多个对象组成的数组:Typescript 如何检查中数组的每个对象中的布尔值是否为false?,typescript,Typescript,我有一个由多个对象组成的数组: quesListArray = [ {Position: 1, Mandatory: false}, {Position: 2, Mandatory: true}, {Position: 3, Mandatory: false}, ... ...
quesListArray = [
{Position: 1, Mandatory: false},
{Position: 2, Mandatory: true},
{Position: 3, Mandatory: false},
...
...
]
如何知道每个对象中的“必填”字段是否为false。如果全部为假,那么我需要显示一条消息
任何帮助都将不胜感激。谢谢。在questListArray
上使用箭头功能(为了简洁起见),如下所示:
areAllMandatoriesFalse() {
if (this.quesListArray.every(item => !item.Mandatory)) {
alert("All are false");
}
else {
alert("Not all are false");
}
}
在questListArray
上使用箭头功能(为简洁起见),如下所示:
areAllMandatoriesFalse() {
if (this.quesListArray.every(item => !item.Mandatory)) {
alert("All are false");
}
else {
alert("Not all are false");
}
}
试试这个:
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;
}
试试这个:
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;
}
使用“每一个”。
例如:
function isBelowThreshold(currentValue) {
return currentValue < 40;
}
var array = [1, 30, 39, 29, 10, 13];
console.log(array.every(isBelowThreshold));
// expected output: true
函数低于阈值(currentValue){
返回电流值<40;
}
var数组=[1,30,39,29,10,13];
log(array.every(isBelowThreshold));
//预期输出:真
我希望我的帮助是有效的ツ 使用“每一个”。
例如:
function isBelowThreshold(currentValue) {
return currentValue < 40;
}
var array = [1, 30, 39, 29, 10, 13];
console.log(array.every(isBelowThreshold));
// expected output: true
函数低于阈值(currentValue){
返回电流值<40;
}
var数组=[1,30,39,29,10,13];
log(array.every(isBelowThreshold));
//预期输出:真
我希望我的帮助是有效的ツ @A先生:如果答案是完整的,请练习将答案向上投票,如果答案是完整的,请将答案标记为已接受。@A先生:如果答案是完整的,请练习将答案向上投票,如果答案是完整的,请将答案标记为已接受。我已经建议使用“每个”。在不到4分钟的时间里,我怀疑我今天在公司遇到了这个问题。我回答了这个答案,我的两位同事喜欢它……:)@VegaI已经建议使用“每个”。在不到4分钟的时间里,我怀疑我今天在公司遇到了这个问题。我回答了这个答案,我的两位同事喜欢它……:)@这个问题和角度有关吗?这个问题和角度有关吗?