for*_*fly 1 javascript arrays arraylist angularjs
我有一个带有属性的数组id和value
var arrayObj= [
{"id": 1, "value": true},
{"id": 2, "value": false},
{"id": 3, "value": true}
]
Run Code Online (Sandbox Code Playgroud)
我需要获取具有属性true/ 的对象数false。我正在使用,Array.prototype.every()但我的逻辑未按预期运行。谁能告诉我我在做什么错?我没有显示计数
function checkIfFalse(value, index, ar) {
document.write(value + " ");
if (value === false)
return true;
else
return false;
}
if (arrayObj.every(checkIfFalse)) {
console.log("all are false");
}else {
console.log("all are true");
}
Run Code Online (Sandbox Code Playgroud)
为什么不使用.filter?
// Your array object
var arrayObj = [
{"id": 1, "value": true},
{"id": 2, "value": false},
{"id": 3, "value": true}
]
// Count true items
var count = arrayObj.filter(function(s) { return s.value; }).length;
// Result
console.log("#True: " + count)Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9570 次 |
| 最近记录: |