【问题标题】:Filter the array in swift for given specific element from the array快速过滤数组中给定的特定元素
【发布时间】:2021-02-17 16:14:37
【问题描述】:

以下是我收到的响应数组。我必须在UITableView 上显示此数据,其中标题将是我的transactionDate,与该日期相关的项目将作为该部分中的行数。

对于下面的代码,我已成功从给定列表中提取出唯一日期,并且在唯一日期数组中获得了 3 个元素。

现在我想要另一个数组,其中包含与唯一日期数组中的日期相关联的项目。

所以结果我应该得到 3 个数组作为 transactionDate 值。 它应该像 array1 有 2 个与日期相关的值对象01/02/2021 array2 将有 1 个与日期相关的值 27/01/2021 和 array3 将有 1 个与日期相关的值对象25/01/2021

随着给定日期的交易继续进行,此数据将继续增加,因此需要一些动态实施。

我的所有数据元素都在数组activityListArray 中,其中包含所有这些元素。 其中我过滤掉了唯一的日期如下,

for i in activityListArray{
            date_array.append(i.transactionDate!)
        }
        let unique = Array(Set(date_array))

现在要获得预期的结果,接下来我应该做什么......?

提前致谢。

我得到的回应如下,

 {
            accountNumber = 0000078;
            amount = 0;
            buyingAmount = "277.42";
            buyingCurrency = EUR;
            currency = "";
            customerInstruction = "0201000009958346-000000218";
            payeeName = "Ash Roy";
            reasonCode = "";
            sellingAmount = "254.00";
            sellingCurrency = GBP;
            status = AWAITINGBANKAPPROVAL;
            subType = "Payment without Fx";
            transactionDate = "01/02/2021";
            type = "";
        },
                {
            amount = 0;
            buyingAmount = "436.39";
            buyingCurrency = GBP;
            currency = "";
            customerInstruction = "0201000009958346-000000210";
            reasonCode = "";
            sellingAmount = "788.00";
            sellingCurrency = CAD;
            status = PENDING;
            subType = "Payment without Fx";
            transactionDate = "27/01/2021";
            type = "";
        },
                {
            amount = 0;
            buyingAmount = "436.39";
            buyingCurrency = GBP;
            currency = "";
            customerInstruction = "0201000009958346-000000207";
            reasonCode = "";
            sellingAmount = "788.00";
            sellingCurrency = CAD;
            status = PENDING;
            subType = "Payment without Fx";
            transactionDate = "25/01/2021";
            type = "";
        },
                {
            amount = 0;
            buyingAmount = "436.39";
            buyingCurrency = GBP;
            currency = "";
            customerInstruction = "0201000009958346-000000206";
            reasonCode = "";
            sellingAmount = "788.00";
            sellingCurrency = CAD;
            status = PENDING;
            subType = "Payment without Fx";
            transactionDate = "01/02/2021";
            type = "";
        }

【问题讨论】:

  • 您可以使用Dictionary(grouping:by) 这样做。

标签: arrays swift xcode filter


【解决方案1】:

您不需要提取唯一日期,Swift 标准库已经提供了一个 API 到 Dictionary,您可以在其中使用 init(grouping:by:) 将对象集合基于特定字段进行分组

在此处阅读所有相关信息https://developer.apple.com/documentation/swift/dictionary/3127163-init

let dict = Dictionary<String,[YourObject]>(grouping: activityListArray, by: {$0.transactionDate})

BTW Dictionary&lt;String,[YourObject]&gt; 不需要显式类型声明,只是实现了它,所以它可以很简单

let dict = Dictionary(grouping: activityListArray, by: {$0.transactionDate})

这将返回一个以唯一 transactionDate 作为键的字典和以该 transactionDate 作为值的对象数组。

现在您可以简单地将部分数返回为dict.keys.count,并将每个部分中的行数返回为dict[Array(dict.keys)[indexPath.section]]?.count

【讨论】:

  • @sagar:请检查发布的答案并让我知道您是否仍然面临问题,如果工作正常,请考虑接受答案
  • @Sandeep Bhandari :- 谢谢哥们,这对我帮助很大,很高兴回答。非常感谢。
  • @sagar:很高兴我能帮上忙 :)
  • @Sandeep Bhandari:- 只是需要快速更新帮助,我们可以使用 transactionDate 以升序或降序对数据进行排序....?任何相同的提示
  • 你可以,只需搜索排序字典键 :) 这应该可以完成工作,请检查我是否可以找到已经回答的问题
猜你喜欢
  • 2014-09-11
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多