我有 NSDictionaries 的 NSMutableArray。这是数组。
Month records (
{
BMI = "209.75";
Date = "15/07/2014 09:03 AM";
Height = 42;
Weight = 37;
day = 15;
month = 7;
year = 2014;
},
{
BMI = "366.67";
Date = "21/07/2014 09:03 AM";
Height = 30;
Weight = 33;
day = 21;
month = 7;
year = 2014;
},
{
BMI = "831.02";
Date = "15/07/2014 09:04 AM";
Height = 19;
Weight = 30;
day = 15;
month = 7;
year = 2014;
}
)
现在我想获取相同“day”的元素(根据这里,“day=15”有 2 个元素,“day=7”有 1 个元素)。然后计算该特定日期的平均 BMI 值。
一般来说,我的要求是获取公共(public)元素(基于键的值)并将它们放入一个新数组中。使用大型数据集在运行时执行此操作的最佳方法是什么。
Best Answer-推荐答案 strong>
int day = /* ... */;
NSArray* recordsForDay = [records filteredArrayUsingPredicate:[NSPredicate predicateWithFormat"day == %d", day]];
或:
NSIndexSet* indexes = [record indexesOfObjectsPassingTest:^BOOL (NSDictionar* record, NSUInteger idx, BOOL *stop) {
return [[record objectForKey"day"] intValue] == day;
}];
NSArray* recordsForDay = [records objectsAtIndexes:indexes];
关于ios - 根据键的值从字典的 NSArray 中获取常用元素,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/25195844/
|