检查值是数组字典值吗? [英] Check if value is in the array as dictionary value?

查看:136
本文介绍了检查值是数组字典值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个方法,如果一个值(存储在一个字典)是一个数组。该阵列看起来像这样: VAR阵列=阵列<&字典LT;字符串,整数>>()

I am looking for a way to see if a value (stored in a dictionary) is in an array. The array looks like this: var array = Array<Dictionary<String, Int>>()

我看了看周围在这里,计算器,发现这样的:的如何检查元素是否是一个数组。唯一的问题是,我似乎无法写入 arrray.contains ... 使用包含方法。我在做什么错了?

I looked around here, on stackoverflow, and found this: How to check if an element is in an array. Only problem is I can't seem to use the contains method by writing arrray.contains.... What am I doing wrong?

推荐答案

您可以使用包含包含来检查是否有在数组的词典包含你正在寻找的价值。

You can use contains within contains to check if any of the dictionaries in an array contains the value you are looking for.

例如,要搜索阵列 A 的值 1

For example, to search array a for the value 1:

let a: [[String: Int]] = [["a": 1, "b": 2], ["c": 3], ["d": 4]]

雨燕1.2:

if contains(a, {contains($0.values, 1)}) {
    println("it is in there")
}

雨燕2.0​​:

/* Thanks to @Qbyte for providing this solution */
if a.contains({ $0.values.contains(1) }) {
    print("it is in there")
}

这工作,因为阵列中的每个成员 A 获取与封闭评估 {包含($ 0.values​​),1} 来看看它是否是真的。闭合需要它是通过字典,会从它值的数组,然后使用包含来看看如果值是在那里。

This works because each member of array a gets evaluated with the closure {contains($0.values), 1} to see if it is true. The closure takes the dictionary it is passed, gets the array of values from it and then uses contains to see if the value is in there.

这篇关于检查值是数组字典值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆