如何根据其中包含的某些值过滤数组(How to filter array on the basis of some value contained within that)
我有如下数组中的对象集合。
arr_recipes = [{ recipe.name = r1, recipe_tags = [t1, t2, t3] }, { recipe.name = r1, recipe_tags = [t1, t2] }, { recipe.name = r1, recipe_tags = [t4, t5, t6] }]现在,我想通过标签名称搜索arr_recipes的内容,即如果我输入t1,它应该返回以下结果:
arr_searchresult= [{ recipe.name = r1, recipe_tags = [t1, t2, t3] }, { recipe.name = r1, recipe_tags = [t1, t2, t3] } }]如果我在uisearhcbar中键入t3,我应该得到如下结果:
arr_searchresult = [{ recipe.name = r1, recipe_tags = [t1, t2, t3] }]任何人都可以建议我在serch结果之上的谓词吗?
I have collection of objects in array as follows.
arr_recipes = [{ recipe.name = r1, recipe_tags = [t1, t2, t3] }, { recipe.name = r1, recipe_tags = [t1, t2] }, { recipe.name = r1, recipe_tags = [t4, t5, t6] }]Now, i want to search the content of arr_recipes by tag name i.e. if I type t1, it should return result below:
arr_searchresult= [{ recipe.name = r1, recipe_tags = [t1, t2, t3] }, { recipe.name = r1, recipe_tags = [t1, t2, t3] } }]If i type, t3 in uisearhcbar, i should get result as below :
arr_searchresult = [{ recipe.name = r1, recipe_tags = [t1, t2, t3] }]Can anyone suggest me the predicate to obtian tha above serch result?
最满意答案
用这个 :
NSArray *arr_recipes = @[@{@"recipe.name" : @"r1", @"recipe_tags" : @[@"t1", @"t2", @"t3"]}, @{@"recipe.name" : @"r1", @"recipe_tags" : @[@"t1", @"t2"] } ]; NSString *searchText = @"t3"; // set your search field value. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recipe_tags contains[cd] %@", searchText]; NSArray *filteredArray = [arr_recipes filteredArrayUsingPredicate:predicate]; NSLog(@"%@", filteredArray);Use this :
NSArray *arr_recipes = @[@{@"recipe.name" : @"r1", @"recipe_tags" : @[@"t1", @"t2", @"t3"]}, @{@"recipe.name" : @"r1", @"recipe_tags" : @[@"t1", @"t2"] } ]; NSString *searchText = @"t3"; // set your search field value. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recipe_tags contains[cd] %@", searchText]; NSArray *filteredArray = [arr_recipes filteredArrayUsingPredicate:predicate]; NSLog(@"%@", filteredArray);更多推荐
发布评论