swift - 使用数组内容过滤数组
问题描述
我想在世界数组中打印属于大陆的名称:“欧洲”
struct countries{
let name: String
let continent: String
}
var world: [countries] = [
countries(name:"japan", continent: "asia"),
countries(name:"france", continent: "europe"),
countries(name:"italy", continent: "europe"),
countries(name:"egypt", continent: "africa")
]
解决方案
world.filter { $0.continent == "europe" }.forEach { print($0.name) }
如果你想要一个名称数组:
world.filter { $0.continent == "europe" }.map { $0.name }
推荐阅读
- c# - 为什么我可以在不指定字符串类的情况下调用 IsNullOrWhiteSpace
- python - 如果python指向python3,python2在哪里?
- javascript - 当我使用 jquery 单击按钮时更改视频 src
- algorithm - 求解 Horn 公式的贪心算法
- javascript - (discord.js) 试图让我的机器人读取用户游戏活动并将语音频道更改为游戏名称
- python-3.x - 如何使用 Pandas 解析时区日期时间对象
- ios - 如何使用 Swift 5 在 SwiftUI 中将颜色与 RGB 或十六进制值一起使用?
- java - 在具有两个 Future 的循环中使用 CompletableFuture 以在每次循环迭代中合并
- github - 将我的项目推送到 GitHub 时遇到问题
- expressionengine - 规范 URL 问题