[英]compare two arrays with not all similar entries and store similar components in third array ios
I have an array arrAll which has a dictionary at each index. 我有一个数组arrAll,在每个索引处都有一个字典。 So, arrAll is of the form
因此,arrAll的形式为
arrAll = ({"id"=1,"type"="first",},{"id"=2,"type"="second"},{"id"=3,"type"="third"},{"id"=4,"type"="fourth"});
I have another array named arrSelected. 我有另一个名为arrSelected的数组。
arrSelected = (second,fourth);
Now, what i have to do is pick the dictionaries containing 'second' & 'fourth' as their type and store them in my third array, say, arrFiltered. 现在,我要做的是选择包含“第二”和“第四”的字典作为它们的类型,并将它们存储在我的第三个数组中,例如arrFiltered。 So ultimately arrFiltered should contain
所以最终arrFiltered应该包含
arrFiltered = ({"id"=2,"type"="second"},{"id"=4,"type"="fourth"});
It could be easily done using conventional method of looping. 使用常规的循环方法可以轻松完成此操作。 I want to ask if there is any other way out to get it done using PREDICATE or something like INTERSECTSET?
我想问一下是否有其他方法可以使用PREDICATE或INTERSECTSET之类的方法来完成它?
For Swift you are looking for something like: 对于Swift,您正在寻找类似的东西:
let arrAll: [[String: Any]] = [
["id": 1, "type": "first"],
["id": 2, "type": "second"],
["id": 3, "type": "third"],
["id": 4, "type": "fourth"]
]
let arrSelected = ["second", "fourth"]
let arrFiltered = arrAll.filter() {
guard let type = $0["type"] as? String else { return false }
return arrSelected.contains(type)
}
print(arrFiltered) // [["id": 2, "type": "second"], ["id": 4, "type": "fourth"]]
Alternative Swift version emanating from arrSelected
rather than arrAll
. 从
arrSelected
而不是arrAll
发出的替代Swift版本。 Pre-requisite that the value for key "type"
is unique in each dictionary in the array (otherwise: just collects first such dictionary): 前提是键
"type"
的值在数组中的每个字典中都是唯一的(否则:仅收集第一个这样的字典):
let arrAll : [[String:Any]] = [
["id": 1, "type": "first"],
["id": 2, "type": "second"],
["id": 3, "type": "third"],
["id": 4, "type": "fourth"]]
let arrSelected = ["second","fourth"]
/* Alternative Swift method: from arrSelected -> filtered */
let arrFiltered = arrSelected.flatMap {
v in arrAll.flatMap { ($0["type"] as? String) == v ? $0 : nil }.first
}
print(arrFiltered)
/* [["id": 2, "type": "second"], ["id": 4, "type": "fourth"]] */
Try out this code for objective-C by using conventional looping method 使用常规循环方法在Objective-C中试用此代码
NSMutableArray * arrFiltered =[[NSMutableArray alloc]init];
for (int i=0; i<arrAll.count; i++)
{
NSDictionary *dictFirst=[arrAll objectAtIndex:i];
for (int j=0; j< arrSelected.count; j++)
{
NSString *strSecond=[arrSelected objectAtIndex:j];
if ([[dictFirst valueForKey:@"type"]isEqualToString:strSecond])
{
[arrFiltered addObject:[arrAll objectAtIndex:i]];
}
}
}
NSLog(@"Flitered Array= %@",arrFiltered);
you can also you predicate to get filtered array by using below code 您也可以通过使用以下代码来确定要过滤的数组
NSMutableArray * arrFiltered =[[NSMutableArray alloc]init];
for (int j=0; j< arrSelected.count; j++)
{
NSString *strSecond=[arrSelected objectAtIndex:j];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type =%@",strSecond];
NSArray *arrTemp=[arrAll filteredArrayUsingPredicate:predicate];
[arrFiltered addObjectsFromArray:arrTemp];
}
NSLog(@"Flitered Array= %@",arrFiltered);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.