【问题标题】:How to filter the Array of Dictionary if the dictionary elements are null?如果字典元素为空,如何过滤字典数组?
【发布时间】:2019-04-12 12:57:55
【问题描述】:

我需要过滤字典数组,但在我的结构中,一些值是null

 "data": [
        {
            "date": "2019-04-12 00:00:00",
            "points": 825,
            "trips": [
                {
                    "id": 108,
                     "trip_detail": “<null>”
    }
        },
        {
            "date": "2019-04-11 00:00:00",
            "points": 2475,
            "trips": [
                {
                    "id": 73,
                     "trip_detail": {
                        "id": 53,
                        "score": 8.25,
                        "points": 825,
                    }
                },
                {
                    "id": 74,
                       "trip_detail": {
                        "id": 54,
                        "score": 8.25,
                        "points": 825,
                    }
                },

所以在上面的结果中我需要删除字典的第一个数组以及那个数组。

【问题讨论】:

  • 1 -&gt; 删除字典的第一个数组以及那个数组请指定你说的是哪个数组? 2 -&gt;需要过滤字典数组根据你想过滤数据的键吗?
  • 如果 trips 对象为 null 我需要删除孔对象 { "date": "2019-04-12 00:00:00", "points": 825, "trips": [ {“id”:108,“trip_detail”:“”}
  • 这似乎是 JSON。首先对其进行序列化。那我们看看吧。你有什么尝试吗?您还想在解析时过滤或跳过它们吗?
  • 我使用了以下 Predicate ,现在可以了 [NSPredicate predicateWithFormat:@"trip_detail != null"]];
  • @Nandhakumar 您需要在问题中显示您的代码。 Edit 问题并添加(作为文本)您的相关代码。清楚地显示您需要帮助的地方;清楚地显示预期和实际结果。

标签: objective-c nsarray nsdictionary


【解决方案1】:

使用过滤器循环一个集合并返回一个数组,该数组只包含那些匹配包含条件的元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多