简体   繁体   English

字典比较数组-swift3

[英]Array of dictionary comparision - swift3

I have 2 array of dictionaries. 我有2个字典。 I want to write a function which compare these 2 arrays. 我想写一个比较这两个数组的函数。 Function should return true only if main array contains sub array element. 仅当主数组包含子数组元素时,函数才应返回true Else it should return false . 否则它应该返回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. 而不是使用for循环进行比较。 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. 如果arrayC为空,则意味着两个数组相等。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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