javascript - How to know if javascript array object values are true?
问题描述
I have a simple array of objects.
{ id: 1, name: 'John', license: true },
{ id: 1, name: 'Sam', license: false },
{ id: 1, name: 'Luis', license: true }
];
How to make a reusable function to check: 1) if license property for all of them together is true 2) if one of license property is true
解决方案
我希望这有帮助
var array = [{ id: 1, name: 'John', license: true },
{ id: 1, name: 'Sam', license: false},
{ id: 1, name: 'Luis', license: true }
];
var trueCounter = 0;
function checkValid(arr){
for(let obj of arr){
if(obj.license == true){
trueCounter++;
}
}
if(trueCounter == array.length){
return "all items are true"
} else if (trueCounter >= 1){
return "at least one item is true"
} else {
return "all items are false"
}
}
console.log(checkValid(array));
推荐阅读
- firebase - FIRESTORE:如何根据条件迭代表
- android - 从谷歌驱动器中选择文件并在android中共享文件
- javascript - 如何在 dc oAuth2 中获得不和谐服务器的角色?
- react-native-image-picker - 安装图像裁剪选择器和调试问题后
- python - 音乐机器人不排队歌曲
- node.js - Discord.js ReferenceError:未定义消息
- rust - 如何正确使用特征对象,可变地迭代可变特征对象容器,同时改变容器本身?
- exoplayer2.x - Exoplayer v2 可以与不同的 IMA 扩展版本一起使用吗?
- c - 字符串比较时堆栈粉碎?
- c++ - 谁能解释如何在 GCC 中使用 OpenACC?