angular - 如何从模板中获取与Angular中数组中的属性匹配的对象计数?
问题描述
假设我有以下数组“myarr”:
[
{'name':'Mary', 'chapter':'Chapter 1'},
{'name':'Joseph', 'chapter':'Chapter 1'},
{'name':'John', 'chapter':'Chapter 2'},
{'name':'Carl', 'chapter':'Chapter 3'},
{'name':'Jacob', 'chapter':'Chapter 3'}
]
我想计算这个数组中标题中有“第 1 章”的对象的数量。有没有办法在 Angular 的 HTML 模板中做到这一点?
现在我从模板中调用一个函数,如下所示:
{{countNumOf('Chapter 1')}}
{{countNumOf('Chapter 2')}}
使用处理 .ts 中的计数的函数...有没有更聪明的方法可以做到这一点,所以我不必在 .ts 中编写额外的函数?
解决方案
如果您可以使用 es6,那么这是最短的方法:
const count = source.filter(item => item.status === '0').length;
或者没有条件,比如只获取对象的数量:
const count = source.filter(item => item).length;
推荐阅读
- linux - 如何为每个命令在文件中重定向 STDOUT 和 STDERR?
- javascript - 如何在 Blogger 上进行 302 重定向
- algorithm - 工作机会的最佳策略
- r-markdown - 在 rmarkdown 中增加目录的宽度
- r - 写成`||`运算符的“条件或”(也称为“短路或”)如何在R中工作?
- ansible - Ansible,在剧本中使用可变内容时出现问题
- google-chrome - 当应用程序应该支持其他浏览器(如 chrome)时应该测试什么?
- r - 在 visIgraphnetwoork 中添加主标题
- jenkins - Jenkins 作业在循环中连续运行
- php - AD/LDAP:单击按钮登录后出现“无效的用户名/密码”