首页 文章

在Swift中过滤Array of Dictinonary of Array中的项目

提问于
浏览
1

我收到的服务回复如下

[["realms": ({
        authorizationType = External;
        contentPageId = "<null>";
        description = "Demo accounts";
        externalRegistrationUrl = "<null>";
        id = 1;
        identifier = Demo;
        showDuringSignup = 1;
    }), "identifier": MENIISRE, "name": MegaTest, "imageData": < null > , "iconFilename": < null > , "imageDataId": 0, "id": 1, "orderIndex": 0, "altName": < null > ],
    ["realms": ({
        authorizationType = Internal;
        contentPageId = "<null>";
        description = Test Desc;
        externalRegistrationUrl = "<null>";
        id = 2;
        identifier = TestFB;
        showDuringSignup = 0;
    }), "identifier": 0012321, "name": TestAccount, "imageData": < null > , "iconFilename": a385bdff - 323 d - 4 a4b - 8019 - 233115 b43b38.png, "imageDataId": 1000019, "id": 2, "orderIndex": 0, "altName": < null > ]]

从上面的回复中,我只能显示' showDuringSignup '为1的记录 .

一个选项是我可以在数组上运行循环并获得所需的结果,但这是一种旧的方式,我想使用高阶函数(例如Filter,Map,flapMap等)

因此,我尝试使用下面的代码

let arrayOfAvailableBanks = arrayOfDictOfBankAccounts.map{$0["realms"].map{($0["showDuringSignup"] as? Int) == 1}}

但我无法得到所需的输入 .

请在这里建议我做错了什么,以及如何只获取'showDuringSignup'为1的那些对象 .

2 回答

  • 2

    好吧,我想我可能已经解码了你提供的回复 .

    你需要的是 filter .

    你可以试试这个:

    let array = // Your response here
    let filtered = array.filter {
        if let realms = $0["realms"] as? [String : Any], 
        let showDuringSignUp = realms["showDuringSignup"] as? Int { // Change Int if necessary
            return showDuringSignUp == 1 // Change compared value as necessary
        }
        return false
    }
    
  • 1

    你应该使用 filter ,请找到下面的代码,

    let arrayOfAvailableBanks = realms.filter { (dict) -> Bool in
        return dict.contains(where: { (arg) -> Bool in
            if arg.key == "realms", let value = arg.value as? [String: Any] {
                if let internalValue = value["showDuringSignup"] as? Int, internalValue == 1 {
                    return true
                }
                return false
            }
            return false
        })
    }
    

    所以整个代码就是这样的

    let realms = [["realms": [
        "authorizationType": "External",
        "contentPageId": nil,
        "description": "Demo accounts",
        "externalRegistrationUrl": "<null>",
        "id": 1,
        "identifier": "Demo",
        "showDuringSignup": 1],
                          "identifier": "MENIISRE", "name": "MegaTest", "imageData": nil , "iconFilename": nil , "imageDataId": 0, "id": 1, "orderIndex": 0, "altName": nil ],
                         ["realms": [
                            "authorizationType": "Internal",
                            "contentPageId": nil,
                            "description": "Test Desc",
                            "externalRegistrationUrl": nil,
                            "id": 2,
                            "identifier": "TestFB",
                            "showDuringSignup": 0],
                          "identifier": 0012321, "name": "TestAccount", "imageData": nil , "iconFilename": "a385bdff - 323 d - 4 a4b - 8019 - 233115 b43b38.png", "imageDataId": 1000019, "id": 2, "orderIndex": 0, "altName": nil ]]
    
    let arrayOfAvailableBanks = realms.filter { (dict) -> Bool in
        return dict.contains(where: { (arg) -> Bool in
            if arg.key == "realms", let value = arg.value as? [String: Any] {
                if let internalValue = value["showDuringSignup"] as? Int, internalValue == 1 {
                    return true
                }
                return false
            }
            return false
        })
    }
    
    print(arrayOfAvailableBanks)
    

相关问题