在字典數組中查找鍵-值對

[英]Find key-value pair in an array of dictionaries


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.

我有兩個字典數組。我要遍歷第一個數組,方法是在第二個數組的字典中搜索1)整個字典或2)鍵值對。

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])'.

如果我試圖搜索整個字典,會得到錯誤:無法找到接受類型為'(數組< dictionary >, [String: String])'的參數列表的'contains'。

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'

如果我只在第二個數組中搜索鍵值對,就會得到:can ' t下標一個' array >'類型為'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()

1 个解决方案

#1


2  

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:

您可以編寫自己的arrayContains函數,它采用您需要的類型:

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!")

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/23/82766e9606bc086029a45ad4ea4085ad.html



 
  © 2014-2022 ITdaan.com 联系我们: