如何根据其中包含的某些值过滤数组(How to filter array on the basis of some value contained within that)

编程入门 行业动态 更新时间:2024-10-27 06:32:21
如何根据其中包含的某些值过滤数组(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);

更多推荐

本文发布于:2023-08-01 13:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   其中包含   filter   contained   basis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!