使用javascript获取数组中真/假值的数量

Get the number of true/false values in an array using javascript

本文关键字:javascript 获取 数组 使用      更新时间:2023-09-26

我有一个属性为idvalue的数组

 var arrayObj=  [
        {"id": 1, "value": true},
        {"id": 2, "value": false},
        {"id": 3, "value": true}
    ]

我需要得到具有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");
}

为什么不用.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);

.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;
 }

因此,arrayObj.every(checkIfFalse)将返回false,因为并非所有项都为假

.every()用于检查数组中的每个项目是否满足特定条件,听起来像是想要获得满足特定条件的项目数量的计数。如果不遍历数组的每个元素并进行检查,就无法做到这一点。如果您只编写自己的for循环,这将是最好的。例:

function countItemsTrue(arry){
   var result = 0;
   for(x = 1; arry.length >= x; x++){
      if(arry[x].value === true){
        result++;
      }
   }
   return result;
}

那么你会做

  var count = CountItemsTrue(arrayObj);

当这样写时,你可以很容易地检查是否都为真,而不需要遍历:

  var allTrue = count == arrayObj.length;

你可以这样做

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);

我不完全确定您实际想要什么:您多次声明要计算有多少项具有false值,但您对代码的讨论使其听起来像是您只想知道它们所有是否具有假value属性。

如果它是您所追求的第一个(假值的实际数量),您可以通过使用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);

如果它是第二个(您想知道是否所有值都为假),或者您想检查是否没有或只有一些条目具有false value条目,您可以通过以下方式实现:

var allFalse = numFalse == data.length,
    noneFalse = numFalse == 0,
    someFalse = numFalse > 0 && numFalse < data.length;

如果你不熟悉reduce,它是这样工作的:

  • 第二个参数为reduce (a 0),将初始计数设置为0。
  • 对于数组中的每个项目,调用传递给reduced的函数,第一个值是当前计数,第二个值是数组中的当前项目。
  • 如果物品的value属性为false,则加1,否则加0。
  • 最后,reduce返回计数的最终值。

var arrayObj = [{
  "id": 1,
  "value": true
}, {
  "id": 2,
  "value": false
}, {
  "id": 3,
  "value": true
}];
var trueCount = falseCount = 0;
arrayObj.forEach(function(object) {
  object.value === true ? trueCount++ : falseCount++;
});
console.log(trueCount, falseCount);

演示

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));

这样做,使用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);