通过特定的映射过滤2个struct / arrays

时间:2017-07-17 16:21:22

标签: arrays json uitableview swift3

我是使用swift 3和xcode 8.3的新用户。目前在控制台输出中过滤2个数组/结构的问题如下:

A_List : Optional([117, 115, 18])

B_List : Optional([{
    URL = "169.jpeg";
    categories = "A";
    description = "description XXX";
    height = 128;
    id = 1;
    likes = "1.00";
    name = "Cake - Baked";
    price = "13.78";
    width = 128;
}, {
    URL = "1622.jpeg";
    categories = "A";
    description = "Baked till golden";
    height = 128;
    id = 15;
    likes = "1.00";
    name = "Croissant";
    price = "3.71";
    width = 128;
}, {
    URL = "11.jpeg";
    categories = "A";
    description = "description Crispy.";
    height = 128;
    id = 18;
    likes = "1.00";
    name = "Plain";
    price = "2.65";
    width = 128;
}, {
    URL = "1622.jpeg";
    categories = "A";
    description = "A ";
    height = 128;
    id = 103;
    likes = "1.00";
    name = "America Pie";
    price = "2.12";
    width = 128;
}, {
    URL = "11.jpeg";
    categories = "B";
    description = "Puff";
    height = 128;
    id = 115;
    likes = "1.00";
    name = "Puff";
    price = "2.12";
    width = 128;
}, {
    URL = "168.jpeg";
    categories = "C";
    description = "description YYY";
    height = 128;
    id = 117;
    likes = "1.00";
    name = "Normal";
    price = "3.18";
    width = 128;
}])

我想将 B_List完整信息作为var filtered_List = [AnyObject]()返回,其中只包含A_List ID号117, 115, and 18,如下所示:

filtered_List : Optional([{
        URL = "11.jpeg";
        categories = "A";
        description = "description Crispy.";
        height = 128;
        id = 18;
        likes = "1.00";
        name = "Plain";
        price = "2.65";
        width = 128;
    }, {
        URL = "11.jpeg";
        categories = "B";
        description = "Puff";
        height = 128;
        id = 115;
        likes = "1.00";
        name = "Mini Puff";
        price = "2.12";
        width = 128;
    }, {
        URL = "168.jpeg";
        categories = "C";
        description = "description YYY";
        height = 128;
        id = 117;
        likes = "1.00";
        name = "Normal";
        price = "3.18";
        width = 128;
    }])

我在youtube上尝试了很少的代码和阅读教程,但遗憾的是没有找到任何解决方案,仅限于swift2示例。

目前,我的代码尝试如下:

var filtered_List = [AnyObject]()
let fullrList = B_List?.map{$0["id"] as! String}.map{_ in A_List}
filtered_List.append(fullrList as AnyObject )
print("result :\(filtered_List)")

非常感谢,如果有专家可以指导或提供您的解决方案。

2 个答案:

答案 0 :(得分:0)

您应该将所需的ID存储在Set中,而不是数组中。您只需要一个简单的filter操作:

let desiredIds: Set = [117, 115, 18]

B_List.filter{ $0["id"].map{ desiredIds.contains($0) } ?? false } as [AnyObject]

答案 1 :(得分:0)

感谢所有向@Alexander特别回复此主题的人。在这里,我分享的解决方案可能不像其他人那样完美。

var resultAnyObject = [AnyObject]()
var aListIds = [String]()

for i in A_List! {
    aListIds.append(i as! String)
}

//@Alexander : thanks for code below:
let desiredIds = aListIds

let fullList = B_List?.filter{ $0["id"].map{desiredIds.contains($0 as! String) } ?? false };

resultAnyObject.append(fullList as AnyObject)

print("Result of filtered_List :\(resultAnyObject)")