javascript 使用javascript获取数组中真/假值的数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32110128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the number of true/false values in an array using javascript
提问by forgottofly
I'm having an Array with properties id
and value
我在与性能的阵列id
和value
var arrayObj= [
{"id": 1, "value": true},
{"id": 2, "value": false},
{"id": 3, "value": true}
]
I need to get the number of objects that has the property true
/false
.
I'm using Array.prototype.every()
but my logic is not working as intended as it seems. Can anyone tell me what I'm doing wrong here? I'm not displaying the count
我需要获取具有属性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");
}
采纳答案by Scott Selby
the .every()
function is expecting being passed an function which will test each item of the array and return true or false. you would want something like:
该.every()
函数期望传递一个函数,该函数将测试数组的每个项目并返回 true 或 false。你会想要这样的:
if (arrayObj.every(checkIfFalse)) {
console.log("all are false");
}
function checkIfFalse(value, index, ar) {
console.log('checking if' + index + ' from array is false');
return value.value == false;
}
so, arrayObj.every(checkIfFalse)
will return false
, because not all items are false
所以,arrayObj.every(checkIfFalse)
会返回false
,因为不是所有的项目都是假的
.every()
is used to check if EVERY item of an array meets a certain condition, it sounds like you want to get a count of the number of items which meet a certain condition. There is no way to do this without iterating through each item of the array and checking. Thhis will be best if you just write your own for loop. ex:
.every()
用于检查数组的每个项目是否满足特定条件,听起来您想获得满足特定条件的项目数量的计数。如果不遍历数组的每个项目并进行检查,就无法做到这一点。如果您只编写自己的 for 循环,这将是最好的。前任:
function countItemsTrue(arry){
var result = 0;
for(x = 1; arry.length >= x; x++){
if(arry[x].value === true){
result++;
}
}
return result;
}
then you would do
那么你会做
var count = CountItemsTrue(arrayObj);
when it's written like this you can easily check if all are true without iterating all over just by checking:
当它这样写时,您可以轻松地检查所有内容是否正确,而无需通过检查来遍历:
var allTrue = count == arrayObj.length;
回答by roland
Why not using .filter
?
为什么不使用.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)
回答by Raghavendra
you can do like this
你可以这样做
int falses=0,trues=0;
for(i=0;i<arrayObj.length;i++) {
if(arrayObj[i].value) {
trues++;
} else {
falses++;
}
}
console.log('Trues: '+trues+' Falses:'+falses);
回答by Dipak Ingole
回答by SuperVeetz
<body>
This is an example.
<script>
var arrayObj= [
{
"id": 1,
"value": true
},
{
"id": 2,
"value": false
},
{
"id": 3,
"value": true
}
];
Array.prototype.numBoolean = function(condition) {
var counter = 0;
for(var i = 0; i < this.length; i++) {
if(this[i].value === condition) {
counter++;
}
}
return counter;
};
console.log(arrayObj.numBoolean(true));
console.log(arrayObj.numBoolean(false));
</script>
</body>
回答by Mac
I'm not entirely sure what you actually want: you state a number of times that you want to count how many items have false
values, yet your discussion of the code makes it sound like you only want to know if they allhave false value
properties.
我不完全确定您真正想要什么:您多次声明要计算有多少项具有false
值,但是您对代码的讨论听起来好像您只想知道它们是否都具有错误的value
属性。
If it's the first that you're after (the actual number of false values), you can get that by using reduce
as follows:
如果它是您所追求的第一个(错误值的实际数量),您可以使用reduce
以下方法获得它:
var arrayObj = [
{ "id": 1, "value": true },
{ "id": 2, "value": false },
{ "id": 3, "value": true }
];
var numFalse = arrayObj.reduce(function(count, item) {
return count + (item["value"] === false ? 1 : 0);
}, 0);
If it's the second (you want to know if allvalues are false), or you want to check if none or only some of the entries have false
value
entries, you can achieve those as follows:
如果是第二个(您想知道所有值是否为假),或者您想检查是否没有条目或只有某些条目有false
value
条目,则可以按如下方式实现:
var allFalse = numFalse == data.length,
noneFalse = numFalse == 0,
someFalse = numFalse > 0 && numFalse < data.length;
If you're not familiar with reduce
, it works like this:
如果您不熟悉reduce
,它的工作方式如下:
- The second parameter to
reduce
(a zero) sets the initial count to zero. - For each item in the array, the function passed to
reduced
is called, with the first value being the current count and the second being the current item in the array. - If the item's
value
property isfalse
, we add one to the count, otherwise we add zero. - At the end,
reduce
returns the final value of the count.
reduce
(a zero)的第二个参数将初始计数设置为零。- 对于数组中的每个项目,调用传递给的函数
reduced
,第一个值是当前计数,第二个值是数组中的当前项目。 - 如果项目的
value
属性是false
,我们将计数加一,否则我们加零。 - 最后,
reduce
返回计数的最终值。
回答by napster
Do like this, use array.forEach instead of native iteration.
这样做,使用 array.forEach 而不是本机迭代。
var arrayObj= [
{
"id": 1,
"value": true
},
{
"id": 2,
"value": false
},
{
"id": 3,
"value": true
}
];
var trueCount = 0, falseCount = 0;
arrayObj.forEach(function(index){
if(index.value == true) {
trueCount++;
}
else if(index.value == false){
falseCount++;
}
});
console.log('True Obj is '+trueCount+' False Obj is '+falseCount);