ios - 查找数组中满足条件的总行数| 迅速
问题描述
本质上,我有一个表视图,其中的行使用 JSON 填充,结构如下:
struct Section {
let name : String
var items : [Portfolio]
}
struct Portfolio: Decodable {
let person: String
let number: String
var checking: Int
enum CodingKeys : String, CodingKey {
case customer, serial, checking
}
}
检查的值可以是 1 或 0
如何计算检查 = 1 的总行数?
目前我正在尝试做这样的事情,但我不确定这是否朝着正确的方向前进:
let item = sections[indexPath.section].items[indexPath.row]
let a = item.checking
let count = a.filter({ $0 % 2 == 0 }).count
解决方案
这是你如何做到的
func totalItems(_ sections: [Section]) -> Int {
return sections.reduce(0) { $0 + $1.items.filter{ $0.checking == 1 }.count }
}
推荐阅读
- typescript - 如何使用 Admin-TS 从 Firebase DB 中检索值
- f# - 如何在 F# 中没有窗口的情况下在屏幕上绘制像素?
- php - 从多维数组中删除每个元素,其中第二个键等于一个变量
- javascript - 是否可以使用 for 循环生成反应道具?
- heroku - 我的免费 webapp dyno 是否有博客,如果有,我如何查看它?
- javascript - 身份验证将我带到个人资料编辑页面
- automationanywhere - 在任何地方以自动化方式杀死谷歌浏览器进程的正确方法
- python - 访问类内常量全局范围变量的 Pythonic 方式?
- javascript - 显示一些标记导致其他标记消失
- c# - DbSet AddOrUpdate 的模拟无法更新我的对象