简体   繁体   中英

Array of dictionary comparision - swift3

I have 2 array of dictionaries. I want to write a function which compare these 2 arrays. Function should return true only if main array contains sub array element. Else it should return false .

Here is my logic-

let mainArry = [ ["id":"1","products":["pid": 1, "name": "A", "price": "$5"]], ["id":"3","products":["pid": 3, "name": "B", "price": "$1"]], ["id":"2","products":["pid": 14, "name": "C", "price": "$15"]]]

let array1 = [ ["id":"1","products":["pid": 1, "name": "A", "price": "$5"]], ["id":"3","products":["pid": 3, "name": "B", "price": "$1"]]]

let array2 =  [ ["id":"1","products":["pid": 1, "name": "A", "price": "$5"]], ["id":"3","products":["pid": 4, "name": "B", "price": "$1"]]]

func compareDictionary(mainArry:[[String: Any]], arr2: [[String: Any]])-> Bool{
    let itemsId = arr2.map { $0["id"]! } // 1, 3, 14

    let filterPredicate = NSPredicate(format: "id IN %@", itemsId)

    let filteredArray = mainArry.filter{filterPredicate.evaluate(with:$0) }

    if filteredArray.count != arr2.count {
        return false
    }
    for obj in filteredArray {
        let prd = obj as Dictionary<String, Any>
        let str = prd["id"] as! String
        let searchPredicate = NSPredicate(format: "id == %@", str )

        let filteredArr = arr2.filter{searchPredicate.evaluate(with:$0) }

        if filteredArr.isEmpty {
            return false
        }

        if !NSDictionary(dictionary: obj["products"] as! Dictionary<String, Any>).isEqual(to: filteredArr.last!["products"] as! [String : Any]) {
            return false
        }

    }
    return true

}

let result1 = compareDictionary(mainArry: mainArry, arr2: array1)
let result2 = compareDictionary(mainArry: mainArry, arr2: array2)

print("Result1 = \(result1)")  // true
print("Result2 = \(result2)")  //false

It is working. But I want to know the best way to achieve this.

Instead of using for-loop for comparision. I want to use filter like this

let arrayC = filteredArray.filter{
        let dict = $0
        return !arr2.contains{ dict == $0 }
    }

if arrayC is empty that means both arrays are equal.

I got it Finally! We don't need to write big function.

let mainArry = [ ["id":"1","products":["pid": 1, "name": "A", "price": "$5"]], ["id":"3","products":["pid": 3, "name": "B", "price": "$1"]], ["id":"2","products":["pid": 14, "name": "C", "price": "$15"]]]

let array1 = [ ["id":"1","products":["pid": 1, "name": "A", "price": "$5"]], ["id":"3","products":["pid": 3, "name": "B", "price": "$1"]]]

let array2 =  [ ["id":"1","products":["pid": 1, "name": "A", "price": "$5"]], ["id":"3","products":["pid": 4, "name": "B", "price": "$1"]]]

let result = array2.filter{
    let dict = $0
    return !mainArry.contains{
        return NSDictionary(dictionary: dict).isEqual(to: $0)
    }
}

if result.isEmpty {
    print("Same key values")
} else {
    print("Diff key values")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM