如何使用对象数组来计算具有active属性为true的元素数量?

huangapple go评论113阅读模式
英文:

With an array of objects, how to count the number elements with active property true?

问题

我不知道问题解决的最佳方法。(过滤器、ng-repeat或任何方法?)

  1. <div>活动元素数量:{{product.length}}</div> <!-- 所有元素的数量 -->
  2. <div>非活动元素数量:{{product.length}}</div> <!-- 所有元素的数量 -->

数组示例:

  1. $scope.product = [
  2. {
  3. "name": "apple",
  4. "id": 1,
  5. "active": false
  6. },
  7. {
  8. "name": "orange",
  9. "id": 2,
  10. "active": true
  11. },
  12. // 其他元素...
  13. ]

如何获取活动产品的数量和非活动产品的数量?哪种方法是最好和简单的?
谢谢!

英文:

I don't know the best way to problem solved.( Filter, ng-repeat or any method?)

  1. &lt;div&gt;Number of active elements: {{product.length}} &lt;/div&gt; //number of all elements
  2. &lt;div&gt;Number of inactive elements: {{product.length}} &lt;/div&gt; //number of all elements

Array example:

  1. $scope.product=[
  2. {
  3. &quot;name&quot;:&quot;apple&quot;,
  4. &quot;id&quot;:1,
  5. &quot;active&quot;:false
  6. },
  7. {
  8. &quot;name&quot;:&quot;orange&quot;,
  9. &quot;id&quot;:2,
  10. &quot;active&quot;:true
  11. },
  12. ....
  13. ]

How to get the number of active products and number of inactive products? Which is the best and simple way?
Thank you!

答案1

得分: 2

只使用AngularJS过滤器然后计数。

  1. <div>活动元素数量:{{(product | filter:{active:true}).length}}</div> //所有元素的数量
  2. <div>非活动元素数量:{{(product | filter:{active:false}).length}}</div> //所有元素的数量

如果你正在过滤字符串并希望应用严格搜索,请使用以下方式。

  1. <div>活动元素数量:{{(product | filter:{active:true}:true).length}}</div> //所有元素的数量
  2. <div>非活动元素数量:{{(product | filter:{active:false}:true).length}}</div> //所有元素的数量
英文:

Just use angularjs filter and then count.

  1. &lt;div&gt;Number of active elements: {{(product | filter:{active:true}).length}} &lt;/div&gt; //number of all elements
  2. &lt;div&gt;Number of inactive elements: {{(product | filter:{active:false}).length}} &lt;/div&gt; //number of all elements

In case you are filtering strings and want to apply a strict search, then use below.

  1. &lt;div&gt;Number of active elements: {{(product | filter:{active:true}:true).length}} &lt;/div&gt; //number of all elements
  2. &lt;div&gt;Number of inactive elements: {{(product | filter:{active:false}:true).length}} &lt;/div&gt; //number of all elements

huangapple
  • 本文由 发表于 2020年1月3日 19:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定