使用数组内容过滤数组

时间:2019-05-17 12:26:47

标签: 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")
]

1 个答案:

答案 0 :(得分:1)

world.filter { $0.continent == "europe" }.forEach { print($0.name) }

如果要使用名称数组:

world.filter { $0.continent == "europe" }.map { $0.name }