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.

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

  • Converting a C char array to a String
  • When is it better to use an NSSet over an NSArray?
  • How to display the values of each array in larger array?
  • Swift JSONDecode decoding arrays fails if single element decoding fails
  • Swift Object reference to array?
  • How to append to an array that is a value in a Swift dictionary
  • 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()
    

    Solutions Collect From Internet About “Find key-value pair in an array of dictionaries”

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