从字典值过滤数组字典

时间:2018-01-22 14:40:14

标签: arrays swift filter

我发现在过滤字典中的字典时遇到了困难。请在下面查看我的回复。

[{
       id = 5a5dae2cb9f9e54a56987185;
       addressId = 0;
       claimStatus = PENDING;
       claimStatusChangedOn = 0;
       gift =         {
           giftAmount = 50;
           giftAmountFormated = "\U20b9 50.00";
           productId = 1

       }
},
{
       id = 5a5dae2cb9f9e54a56987185;
       addressId = 0;
       claimStatus = PENDING;
       claimStatusChangedOn = 0;
       gift =         {
           giftAmount = 50;
           giftAmountFormated = "\U20b9 50.00";
           productId = 2

       }
},
{
       id = 5a5dae2cb9f9e54a56987185;
       addressId = 0;
       claimStatus = PENDING;
       claimStatusChangedOn = 0;
       gift =         {
           giftAmount = 50;
           giftAmountFormated = "\U20b9 50.00";
           productId = 3

       }
},
{
       id = 5a5dae2cb9f9e54a56987185;
       addressId = 0;
       claimStatus = PENDING;
       claimStatusChangedOn = 0;
       gift =         {
           giftAmount = 50;
           giftAmountFormated = "\U20b9 50.00";
           productId = 1

       }
},
{
       id = 5a5dae2cb9f9e54a56987185;
       addressId = 0;
       claimStatus = PENDING;
       claimStatusChangedOn = 0;
       gift =         {
           giftAmount = 50;
           giftAmountFormated = "\U20b9 50.00";
           productId = 1
       }
}]

我希望按gifts -> productId过滤不同的数组。

例如,array1将是productId = 1的字典, array2将包含productId = 2的字典 等

我无法理解缩小,过滤和深度地图。

1 个答案:

答案 0 :(得分:0)

我刚刚得到它。我的问题不明确。我想通过giftId在礼品字典中对数组进行分组。

这是我发现的:

let groupDict = Dictionary(grouping:array){ (dictionaryObj) -> String in
    return dictionaryObj.gift.productId
}