【问题标题】:How to get an array(say arrayGroupBy) of dictionary from array of dictionary, where some key’s value are matching in another dictionary key’s value如何从字典数组中获取字典数组(比如arrayGroupBy),其中某个键的值与另一个字典键的值匹配
【发布时间】:2014-12-31 11:00:38
【问题描述】:

我有一个包含 NSDictionaries 的 NSMutable 数组,如下所示:

<__NSArrayM 0x7a2b4b00>(
{
"appointment_id" = 235;
"component_name" = Anil;
id = 5;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
},
{
    "appointment_id" = 235;
"component_name" = "test_book";
id = 19;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 235;
"component_name" = "New Resource";
id = 21;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 236;
"component_name" = ctbl;
id = 8;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
},
{
"appointment_id" = 236;
"component_name" = btbl;
id = 7;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
  },
{
"appointment_id" = 236;
    "component_name" = "New Resource";
id = 21;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
}
)

我如何从上面的结构中创建一个新数组,比如 arrGroupByTimeSlots:

array of time slot
(
time_slot = (an array of dictionaries with matching time_slot)
time_slot = (an array of dictionaries with matching another time_slot)
)

详细说明:

(
time_slot = ({
"appointment_id" = 235;
"component_name" = Anil;
id = 5;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
}, {
"appointment_id" = 235;
"component_name" = "test_book";
id = 19;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
}, {
"appointment_id" = 235;
"component_name" = "New Resource";
id = 21;
"start_time" = "2014-11-04 19:00:00";
"time_slot" = "7:00 PM";
}),

time_slot = ({
"appointment_id" = 236;
"component_name" = ctbl;
id = 8;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
}, {
"appointment_id" = 236;
"component_name" = btbl;
id = 7;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
}, {
"appointment_id" = 236;
"component_name" = "New Resource";
id = 21;
"start_time" = "2014-11-04 22:00:00";
"time_slot" = "10:00 PM";
}))

我尝试过的:Onik IV 提供的解决方案。

更多日志:注意 "time_slot" = "7:00 PM"; 我要排序的字典数组:

Printing description of arrAllocated:
<__NSArrayM 0x19055f70>(
{
"appointment_id" = 244;
"component_name" = "badge test";
id = 20;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p1;
id = 42;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p2;
id = 41;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p3;
id = 43;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
}, 
{
"appointment_id" = 247;
"component_name" = p4;
id = 44;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 247;
"component_name" = p5;
id = 45;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r1;
id = 29;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r10;
id = 38;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r11;
id = 39;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r12;
id = 40;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r2;
id = 30;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r3;
id = 31;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r4;
id = 32;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r5;
id = 33;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r6;
id = 34;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r7;
id = 35;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r8;
id = 36;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r9;
id = 37;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
}
)

使用 Onik IV 方法排序后,我开始关注 "time_slot" = "7:00 PM";:

<__NSArrayM 0x17e9b660>(
<__NSArrayM 0x19088e80>(
{
"appointment_id" = 244;
"component_name" = "badge test";
id = 20;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
}
)
,
<__NSArrayM 0x190543b0>(
{
"appointment_id" = 246;
"component_name" = p1;
id = 42;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p2;
id = 41;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
},
{
"appointment_id" = 246;
"component_name" = p3;
id = 43;
"start_time" = "2014-11-11 18:00:00";
"time_slot" = "6:00 PM";
}
)
,
<__NSArrayM 0x1905d750>
(
{
"appointment_id" = 247;
"component_name" = p4;
id = 44;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
},
{
"appointment_id" = 247;
"component_name" = p5;
id = 45;
"start_time" = "2014-11-11 19:00:00";
"time_slot" = "7:00 PM";
}
)
,
<__NSArrayM 0x190243e0>
(
{
"appointment_id" = 245;
"component_name" = r1;
id = 29;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r10;
id = 38;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r11;
id = 39;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r12;
id = 40;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r2;
id = 30;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r3;
id = 31;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r4;
id = 32;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r5;
id = 33;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r6;
id = 34;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r7;
id = 35;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r8;
id = 36;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";
},
{
"appointment_id" = 245;
"component_name" = r9;
id = 37;
"start_time" = "2014-11-11 17:00:00";
"time_slot" = "5:00 PM";

} )

)

如果您看到 "time_slot" = "7:00 PM";

它没有分组在同一个数组中,其中 other "time_slot" = "7:00 PM";被分组。

希望我能说得更清楚。 谢谢和问候。

【问题讨论】:

  • 你有没有尝试过?
  • 是的,我已经尝试过使用谓词查看编辑
  • 必须是字典吗?你不能改用自定义对象吗?
  • 另外,如果有两个时间段相同(晚上 7 点)但在不同的日期怎么办?它们是否被视为相同?
  • @Fogmeister 感谢您的回答,是的,目前根据项目要求(我们不管理此场景的数据模型),我必须对这本字典进行排序。

标签: objective-c iphone ipad nsarray nsdictionary


【解决方案1】:

我使用这样的东西:

NSArray *yourArray = /* Initialize */;
// Get all time slots
NSArray *allTimeSlotsArray = [yourArray valueForKey:@"time_slot"];
// Remove duplucates
NSSet *allTimeSlotsSet = [NSSet setWithArray:allTimeSlotsArray];
  // Enumerate
NSMutableDictionary *recordsByTimeSlot = [NSMutableDictionary dictionary];
for (NSString *timeSlot in allTimeSlotsSet) {
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"time_slot == %@", timeSlot];
  NSArray *matchingArray = [yourArray filteredArrayUsingPredicate:predicate];
  recordsByTimeSlot[timeSlot] = matchingArray;
}

更新:
存储 matchingArray 或做任何你需要的事情。
使用生成的 recordsByTimeSlot 字典。

【讨论】:

    【解决方案2】:

    这里有一个方法:

    NSArray *arrAllocated; // Your initial array
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"time_slot" ascending:YES];
    NSArray *sorts = @[sort];
    NSArray *orderedArrAllocated = [arrAllocated sortedArrayUsingDescriptors:sorts];
    
    NSMutableArray *groupedFilterArray = [[NSMutableArray alloc] init];
    NSMutableArray *currentMutableArray;
    NSDictionary *lastObject;
    
    for (NSDictionary *dict in orderedArrAllocated)
    {
        if ([[dict valueForKey:@"time_slot"] isEqualToString:[lastObject valueForKey:@"time_slot"]])
        {
            [currentMutableArray addObject:dict];
        }
        else
        {
            NSMutableArray *newMutableArray = [[NSMutableArray alloc] initWithObjects:dict, nil];
            lastObject = dict;
            currentMutableArray = newMutableArray;
            [groupedFilterArray addObject:newMutableArray];
    
        }
    }
    

    【讨论】:

    • 感谢您的回答,它对某些词典进行了分组,但对其他词典没有进行分组
    • @Alok 请解释这里不起作用的地方。当您说它不是将其他人分组时。你到底什么意思?还有哪些?正要写答案,才发现和这个一模一样。
    • @Fogmeister 让我调试并为您提供日志
    • @Alok 您没有正确实施此解决方案。具体来说,您在开始时没有对数组进行排序。此解决方案的前 4 行按时隙顺序对数组进行排序。必须这样做,以使相似的时隙彼此相邻。
    • @Alok 请注意,您不会更改初始数组的顺序。您正在创建一个已排序的 新数组。此方法未触及初始数组。
    【解决方案3】:

    @orkenstein 的答案在这里完美运行。但唯一的问题是,如果您不保存匹配的数组,它将被覆盖。为此,您可以使用 NSMutableArray 并在 for 循环中添加匹配的数组。我没有足够的声誉将其作为评论发布,因此我将其发布为答案。 :)

    -(void)filteredArray
    {
          NSArray *yourArray = originalArray;
          // Get all time slots
          NSArray *allTimeSlotsArray = [yourArray valueForKey:@"time_slot"];
          // Remove duplucates
          NSSet *allTimeSlotsSet = [NSSet setWithArray:allTimeSlotsArray];
          // Enumerate
        for (NSString *timeSlot in allTimeSlotsSet) {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"time_slot == %@", timeSlot];
            NSArray *matchingArray = [yourArray filteredArrayUsingPredicate:predicate];
            [finalArray addObjectsFromArray:matchingArray];
        }
        NSLog(@"Filtered Array : %@",finalArray);
    }
    

    finalArray 是一个 NSMutableArray 对象。

    【讨论】:

    • 我觉得太明显了)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多