你可以这样走。
let dic = ["Olivia":[2.0, 0.0, 1.0, 3.0],"Amber":[60.0, 0.0, 0.0, 1.0]]
let filterKeys = dic.flatMap { $0.value.first == 60 ? $0.key : nil }
//If you are having only one pair like that then simply access the first object from array
print(filterKeys.first)
//Other wise you can access the whole array
print(filterKeys)
注:如果你想检查ArrayDouble包含特定的值,而不是仅仅比较,你可以尝试这样的第一要素。
let filterKeys = dic.flatMap { $0.value.contains(60) ? $0.key : nil }
//If you are having only one pair like that then simply access the first object from array
print(filterKeys.first)
//Other wise you can access the whole array
print(filterKeys)
编辑:如果你想检查数组包含> 60
对于您可以使用contains(where:)
对象。
let filterKeys = dic.flatMap { $0.value.contains(where: { $0 > 60}) ? $0.key : nil }
如果我想检查其他值怎么办?或者使条件像> 60?那可能吗? – kings077712
@ kings077712你的意思是检查任何数组的值,并检查它是否> 60或不?或者只比较数组的第一个对象? –
是的,而不只是比较第一个对象! – kings077712