减去数组,关于字典中的键
Subtracting the array, Regarding key in dictionary
假设我有两个数组
NSArray *array1=@[ @{@"key1":@"A",@"key2":@"AA"},@{@"key1":@"C",@"key2":@"CC"},@{@"key1":@"E",@"key2":@"EE"},@{@"key1":@"G",@"key2":@"GG"}];
NSArray *array2=@[ @{@"key1":@"A",@"key2":@"AAA"},@{@"key1":@"Z",@"key2":@"ZZZ"}];
我想减去数组,这应该是预期的结果,
NSArray *resultArray=@[ @{@"key1":@"C",@"key2":@"CC"},@{@"key1":@"E",@"key2":@"EE"},@{@"key1":@"G",@"key2":@"GG"}];
我试过下面的代码但没有用
NSArray *extracted = [array1 valueForKey:@"key1"];
NSMutableSet *pressieContactsSet = [NSMutableSet setWithArray:extracted];
NSMutableSet *allContactSet = [NSMutableSet setWithArray:array2];
[allContactSet minusSet:pressieContactsSet];
NSLog(@"%@",allContactSet);
这应该有效。
NSArray *array1=@[ @{@"key1":@"A"},@{@"key1":@"C"},@{@"key1":@"E"},@{@"key1":@"G"}];
NSArray *array2=@[ @{@"key1":@"A"},@{@"key1":@"Z"}];
NSMutableSet *pressieContactsSet = [NSMutableSet setWithArray:array1];
NSSet *allContactSet = [NSSet setWithArray:array2];
[pressieContactsSet minusSet:allContactSet];
NSArray *result = [pressieContactsSet allObjects];
NSLog(@"%@",result);
尽情享受吧!
NSArray *array1=@[ @{@"key1":@"A"},@{@"key1":@"C"},@{@"key1":@"E"},@{@"key1":@"G"}];
NSArray *array2=@[ @{@"key1":@"A"},@{@"key1":@"Z"}];
NSMutableArray *resultArray = [NSMutableArray arrayWithArray:array1];
[resultArray removeObjectsInArray:array2];
NSLog(@"array %@",resultArray);
请尝试以下代码
首先获取临时数组中的所有key1对象。然后在 array1 上应用过滤器并检查您的 array1 对象是否包含 arrayKey1 对象。
确保它只会检查 key1
键。
NSArray *arrKey1 = [array2 valueForKey:@"key1"];
NSPredicate *pred = [NSPredicate predicateWithBlock:
^BOOL(id evaluatedObject, NSDictionary *bindings)
{
if ([arrKey1 containsObject:evaluatedObject[@"key1"]])
{
NSLog(@"found : %@",evaluatedObject);
return NO;
}
else
{
NSLog(@"Not found : %@",evaluatedObject);
return YES;
}
}];
NSArray *arrSubtracted = [array1 filteredArrayUsingPredicate:pred];
NSLog(@"%@", arrSubtracted);
或者您可以使用 enumerateObjectsUsingBlock
NSMutableArray *resultArray = [NSMutableArray new];
[array1 enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
{
if (![arrKey1 containsObject:obj[@"key1"]]) {
[resultArray addObject:obj];
}
}];
NSLog(@"%@",resultArray);
希望对您有所帮助。
假设我有两个数组
NSArray *array1=@[ @{@"key1":@"A",@"key2":@"AA"},@{@"key1":@"C",@"key2":@"CC"},@{@"key1":@"E",@"key2":@"EE"},@{@"key1":@"G",@"key2":@"GG"}];
NSArray *array2=@[ @{@"key1":@"A",@"key2":@"AAA"},@{@"key1":@"Z",@"key2":@"ZZZ"}];
我想减去数组,这应该是预期的结果,
NSArray *resultArray=@[ @{@"key1":@"C",@"key2":@"CC"},@{@"key1":@"E",@"key2":@"EE"},@{@"key1":@"G",@"key2":@"GG"}];
我试过下面的代码但没有用
NSArray *extracted = [array1 valueForKey:@"key1"];
NSMutableSet *pressieContactsSet = [NSMutableSet setWithArray:extracted];
NSMutableSet *allContactSet = [NSMutableSet setWithArray:array2];
[allContactSet minusSet:pressieContactsSet];
NSLog(@"%@",allContactSet);
这应该有效。
NSArray *array1=@[ @{@"key1":@"A"},@{@"key1":@"C"},@{@"key1":@"E"},@{@"key1":@"G"}];
NSArray *array2=@[ @{@"key1":@"A"},@{@"key1":@"Z"}];
NSMutableSet *pressieContactsSet = [NSMutableSet setWithArray:array1];
NSSet *allContactSet = [NSSet setWithArray:array2];
[pressieContactsSet minusSet:allContactSet];
NSArray *result = [pressieContactsSet allObjects];
NSLog(@"%@",result);
尽情享受吧!
NSArray *array1=@[ @{@"key1":@"A"},@{@"key1":@"C"},@{@"key1":@"E"},@{@"key1":@"G"}];
NSArray *array2=@[ @{@"key1":@"A"},@{@"key1":@"Z"}];
NSMutableArray *resultArray = [NSMutableArray arrayWithArray:array1];
[resultArray removeObjectsInArray:array2];
NSLog(@"array %@",resultArray);
请尝试以下代码
首先获取临时数组中的所有key1对象。然后在 array1 上应用过滤器并检查您的 array1 对象是否包含 arrayKey1 对象。
确保它只会检查 key1
键。
NSArray *arrKey1 = [array2 valueForKey:@"key1"];
NSPredicate *pred = [NSPredicate predicateWithBlock:
^BOOL(id evaluatedObject, NSDictionary *bindings)
{
if ([arrKey1 containsObject:evaluatedObject[@"key1"]])
{
NSLog(@"found : %@",evaluatedObject);
return NO;
}
else
{
NSLog(@"Not found : %@",evaluatedObject);
return YES;
}
}];
NSArray *arrSubtracted = [array1 filteredArrayUsingPredicate:pred];
NSLog(@"%@", arrSubtracted);
或者您可以使用 enumerateObjectsUsingBlock
NSMutableArray *resultArray = [NSMutableArray new];
[array1 enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
{
if (![arrKey1 containsObject:obj[@"key1"]]) {
[resultArray addObject:obj];
}
}];
NSLog(@"%@",resultArray);
希望对您有所帮助。