简体   繁体   English

如何从字典数组中获取字典的数组(例如arrayGroupBy),其中某个键的值与另一个字典键的值匹配

[英]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

i have an NSMutable arrray containing NSDictionaries like this: 我有一个包含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";
}
)

how do i create a new array say arrGroupByTimeSlots from above structure some thing like this: 我如何从上面的结构中创建一个新数组说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)
)

in detail: 详细:

(
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";
}))

what i have tried: solution provided by Onik IV as it is. 我尝试过的是: Onik IV提供的解决方案。

some more log:pay attention to "time_slot" = "7:00 PM"; 更多日志:注意“ time_slot” =“ 7:00 PM”; the array of dictionaries i want to sort : 我要排序的字典数组:

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";
}
)

after sorting using Onik IV method this is what i am getting pay attention to "time_slot" = "7: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";

} ) })

)

if you see "time_slot" = "7:00 PM"; 如果您看到“ time_slot” =“ 7:00 PM”;

it is not grouped in the same array where other "time_slot" = "7:00 PM"; 它不在其他“ time_slot” =“ 7:00 PM”的同一数组中分组; are grouped. 被分组。

hope i am able to make it more clear. 希望我能说得更清楚。 thanks and regards. 谢谢并恭祝安康。

I use something like this: 我用这样的东西:

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;
}

UPDATED: 更新:
Store matchingArray or do whatever you need. 存储matchingArray或执行您所需的任何操作。
Use the resulting recordsByTimeSlot dictionary. 使用生成的recordsByTimeSlot字典。

Here is a way: 这是一种方法:

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];

    }
}

@orkenstein answer works perfectly here. @orkenstein答案在这里非常有效。 But the only issue is if you dont save the matching array it will be overwritten. 但是唯一的问题是,如果不保存匹配的数组,它将被覆盖。 For that you can use an NSMutableArray and add the matching array inside the for loop. 为此,您可以使用NSMutableArray并将匹配的数组添加到for循环中。 I dont have enough reputation to post this as a comment, so I'm posting it as an answer. 我没有足够的声誉将其发布为评论,所以我将其发布为答案。 :) :)

-(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 is an NSMutableArray object. finalArray是一个NSMutableArray对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM