简体   繁体   English

如何获取对象数组中布尔值的计数(如果为真)

[英]How to get the count of boolean value (if it is true) in array of objects

I have to get the count of boolean value (if it is true) in array of objects.我必须在对象数组中获取布尔值的计数(如果为真)。 The json structure is given below: json结构如下:

[
  {
    "id": 5,
    "name": "a",
    "select": true
  },

  {
    "id": 3,
    "name": "b",
    "select": false
  },

  {
    "id": 2,
    "name": "x",
    "select": true
  },

  {
    "id": 1,
    "name": "y",
    "select": false
  }
]

You can do it using Array.prototype.filter()你可以使用Array.prototype.filter()

Try like this像这样尝试

console.log(data.filter((x,i) => { return x.select; }).length)

DEMO

That should do the trick.那应该可以解决问题。 no angular needed不需要角度

function getTrueCount(array) {
  var count = 0;
  for (var i = 0; i < array.length; i++) {
    if (array[i].select) {
      count++;
    }
  }
  return count;
}

You can do it using filter .您可以使用filter来做到这一点。 The code would be:代码将是:

var truevalues = yourarray.filter(function(element) {
    return (element.select == true);
}

It returns the values that accomplish the condition, so the values that have select true.它返回满足条件的值,因此选择 true 的值。 Then you can count the values using truevalues.length然后您可以使用truevalues.length计算值

This is the functional programming way of doing it:这是执行此操作的函数式编程方式:

var count = yourarray.reduce((a, c) => c.yourboolvariable ? ++a : a, 0);

Explanation: The zero at the end is the initial value of a.说明:末尾的零是a的初始值。 a is known as the accumulator. a 称为累加器。 The "reduce" function iterates through each value in the array, passing in the accumulator "a" as well is the current item "c". “reduce”函数遍历数组中的每个值,传入累加器“a”以及当前项“c”。 The right size of the fat arrow is the return value.粗箭头的正确大小是返回值。 If yourboolvariable value is true, it will add 1 to a and return that.如果 yourboolvariable 值为真,它会将 1 添加到 a 并返回。 If yourboolvariable value is false, it will simply return the previous value of a, ready to be passed in to the next iteration.如果您的boolvariable 值为false,它将简单地返回a 的前一个值,准备传递给下一次迭代。 The final value of a becomes the result returned to count. a 的最终值成为返回给 count 的结果。

This is not angular.这不是角。 This is straight JavaScript.这是直接的 JavaScript。

so , If it is response then you can use forEach loop like所以,如果是响应,那么你可以使用 forEach 循环

$scope.count = 0;
angular.forEach(response, function(value, key) {
  if (value.select == true) {
    $scope.count = $scope.count + 1;
  }
});

Then u can take $scope.count from here然后你可以从这里获取$scope.count

If you have a specific JSON structure which you have mentioned then you can try something like this.如果你有一个你提到的特定 JSON 结构,那么你可以尝试这样的事情。 Can use arrow function if you want如果需要,可以使用箭头功能

const getResult = function(inputArr){
    let result = [];
    if( Object.prototype.toString.call(inputArr) === '[object Array]' && inputArr.length > 0 ){
        result = inputArr.filter(function(item){
            return item.select;
        });
    }
    return result.length;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM