我有一系列以下格式的词典
[
{
"status":"not completed",
"rating":2
},{
"status":"completed",
"rating":2
},{
"status":"not completed",
"rating":"<null>"
},{
"status":"completed",
"rating":"<null>"},{
"status":"not completed",
"rating":"<null>"
}
]
从此我希望响应作为状态完成且评级为空的最后一个对象
output
{
"status":"completed",
"rating":"<null>"
}
如何在没有任何大迭代的情况下轻松获得第二个元素。
在实际情况中,此数组可能包含150-300个元素。
-(void)checkRecentUnratedOrder:(NSMutableArray *)order{
NSLog(@"trip log - %@",[order valueForKey:@"status"]); // total 5 objects inside array in that 4 of them are completed
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == completed"];
NSArray *arr = [order filteredArrayUsingPredicate:predicate];
NSLog(@"trip arr%@",arr);
}
答案 0 :(得分:1)
您需要predicate
这样的数组
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.status == 'completed'"];
NSArray *arr = [order filteredArrayUsingPredicate:predicate];
// Now `arr` contains all the `dictionary` that `status` key contain `completed` value.