找到键值对字典中的数组 [英] Find key-value pair in an array of dictionaries

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

问题描述

我的字典两个数组。我想通过第二阵列在字典中搜索任一1)整个词典或2)一个键 - 值对通过所述第一阵列进行迭代。

如果我尝试搜索整个字典,我得到错误:为'包含'接受数组类型和LT的'(参数列表无法找到过载;字典<字符串,字符串>> [字符串:字符串]。)

 对于VAR I = 0; I< arrayOne.count;我++ {    在arrayOne {项目        如果包含(arrayTwo,arrayOne [I])== {为零            // 做一点事        }
    }
}

如果我尝试搜索只是第二个数组中的键值对,我得到:无法下标类型的值数组<字典<字符串,字符串>>'与类型的索引'字符串'

 在arrayOne {项目     如果包含(arrayTwo,arrayOne [标题]){         // 做一点事     }
}

我需要能够搜索词典的第二阵列为字典的第一阵列中的一些值。我该怎么做?

编辑:两个数组有相同的声明:

  VAR arrayOne:数组<&字典LT;字符串,字符串>> =阵列()
VAR arrayTwo:数组<&字典LT;字符串,字符串>> =阵列()


解决方案

下面的作品。可以通过每个阵列的项目迭代比较它们中的其他的项

 在arrayOne ITEM1 {
    在arrayTwo {ITEM2
        若ITEM1 == {ITEM2
            的println(发现常见的两种阵列词典)
        }
    }
}

在搜索公共密钥/在两个数组值对:

 在arrayOne {dict1
    在dict1(键,值){
        在arrayTwo dict2 {
            如果dict2 [关键] == {值
                的println(发现\\(键):\\(值)在两个数组)
            }
        }
    }
}

如果你正在寻找一个特定的键:

 让键=标题在arrayOne dict1 {
    如果让值= dict1 [关键] {
        在arrayTwo dict2 {
            如果dict2 [关键] == {值
                的println(发现\\(键):\\(值)在两个数组)
            }
        }
    }
}


您可以编写自己的 arrayContains 函数,它接受你所需要的类型:

  FUNC arrayContains(数组:[[字符串:字符串],值:[字符串:字符串]) -  GT;布尔{
    在数组项{
        如果项目== {值
            返回true
        }
    }
    返回false
}

,然后用它来找到你想要的值:

  //保存所有从第一阵列是不是第二个数组中的字典VAR newArray:[字符串:字符串]] = []在arrayOne {项目
    如果!arrayContains(arrayTwo,项目){
        newArray.append(项目)
    }
}


要搜索阵列特定键/值对:

  FUNC arrayContains(数组:[[字符串:字符串],#键:字符串,#VALUE:字符串) -  GT;布尔{
    在阵列字典{
        如果字典[关键] == {值
            返回true
        }
    }
    返回false
}让结果= arrayContains(arrayTwo,键:标题,价值:异形!)

I have two arrays of dictionaries. I want to iterate through the first array by searching for either 1) the entire dictionary or 2) a key-value pair within a dictionary in the second array.

If I try to search for the entire dictionary, I get error: Cannot find an overload for 'contains' that accepts an argument list of type '(Array<Dictionary<String, String>>, [String : String])'.

for var i = 0; i < arrayOne.count; i++ {

    for item in arrayOne {

        if contains(arrayTwo, arrayOne[i]) == nil {

            // Do something

        }
    } 
}

And if I try to search for just a key-value pair within the second array, I get: Cannot subscript a value of type 'Array<Dictionary<String, String>>' with an index of type 'String'

for item in arrayOne {

     if contains(arrayTwo, arrayOne["title"]) {

         // Do something

     }
}

I need to be able to search the second array of dictionaries for some value in the first array of dictionaries. How can I do that?

EDIT: Both arrays have the same declarations:

var arrayOne : Array<Dictionary<String, String>> = Array()
var arrayTwo : Array<Dictionary<String, String>> = Array()

解决方案

The following works. You can iterate through the items of each array comparing them to the items in the other:

for item1 in arrayOne {
    for item2 in arrayTwo {
        if item1 == item2 {
            println("found a dictionary common to both arrays")
        }
    }
}

Searching for common key/value pairs in the two arrays:

for dict1 in arrayOne {
    for (key, value) in dict1 {
        for dict2 in arrayTwo {
            if dict2[key] == value {
                println("found \(key):\(value) in both arrays")
            }
        }
    }
}

And if you're searching for a particular key:

let key = "title"

for dict1 in arrayOne {
    if let value = dict1[key] {
        for dict2 in arrayTwo {
            if dict2[key] == value {
                println("found \(key):\(value) in both arrays")
            }
        }
    }
}


You can write your own arrayContains function that takes the types you need:

func arrayContains(array:[[String:String]], value:[String:String]) -> Bool {
    for item in array {
        if item == value {
            return true
        }
    }
    return false
}

And then use it to find the values you want:

// Save all of the dictionaries from the first array that aren't in the second array

var newArray:[[String:String]] = []

for item in arrayOne {
    if !arrayContains(arrayTwo, item) {
        newArray.append(item)
    }
}


To search the array for a particular key/value pair:

func arrayContains(array:[[String:String]], #key: String, #value: String) -> Bool {
    for dict in array {
        if dict[key] == value {
            return true
        }
    }
    return false
}

let result = arrayContains(arrayTwo, key: "title", value: "Alien!")

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

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