将字典数组拆分为子数组
Split array of dictionaries into sub-arrays
下面是我的 JSON 回复的一部分,其中“results
”是一个字典数组:
{
"results": [
{
"id": 6,
"genre_name": "Action",
"cover_image": "http://54.254.204.81/images/Action.png"
},
{
"id": 5,
"genre_name": "Adventure",
"cover_image": "http://54.254.204.81/images/Adventure.png"
},
{
"id": 4,
"genre_name": "Romance",
"cover_image": "http://54.254.204.81/images/Romance.png"
},
{
"id": 3,
"genre_name": "Sci-Fci",
"cover_image": "http://54.254.204.81/images/Sci-Fi.png"
},
{
"id": 1,
"genre_name": "Guide",
"cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
},
{
"id": 2,
"genre_name": "Horror",
"cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
},
{
"id": 7,
"genre_name": "Emotional",
"cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
},
{
"id": 8,
"genre_name": "abcd",
"cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
}
]
}
现在我有另一个 JSON 响应,其中“genres
”是一个数组,其中包含的对象是具有键 'id' 的“results
”数组对象的子集。
{
"genres": [
3,
1
]
}
现在,我是否可以将“results
”拆分为两个数组“results1
”和“results2
”,例如:
{
"results1": [
{
"id": 6,
"genre_name": "Action",
"cover_image": "http://54.254.204.81/images/Action.png"
},
{
"id": 5,
"genre_name": "Adventure",
"cover_image": "http://54.254.204.81/images/Adventure.png"
},
{
"id": 4,
"genre_name": "Romance",
"cover_image": "http://54.254.204.81/images/Romance.png"
},
{
"id": 2,
"genre_name": "Horror",
"cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
},
{
"id": 7,
"genre_name": "Emotional",
"cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
},
{
"id": 8,
"genre_name": "abcd",
"cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
}
]
}
和
{
"results2": [
{
"id": 3,
"genre_name": "Sci-Fci",
"cover_image": "http://54.254.204.81/images/Sci-Fi.png"
},
{
"id": 1,
"genre_name": "Guide",
"cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
}
]
}
为此,您需要像这样将 NSPredicate
与 IN
和 NOT
一起使用。
首先根据您的两个 JSON 回复创建 resultArray
和 genresArray
。
NSArray *resultArray = [firstJSONResponse objectForKey:@"results"];
NSArray *genresArray = [secondJSONResponse objectForKey:@"genres"];
现在使用 NSPredicate
过滤 resultArray
以获得结果。
对于结果 1
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (id IN %@)",genresArray];
NSArray *firstSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",firstSplitArray);
对于结果 2
predicate = [NSPredicate predicateWithFormat:@"id IN %@",genresArray];
NSArray *secondSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",secondSplitArray);
喜欢以下方式:
NSMutableArray * genresArray = [NSMutableArray new];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (id IN %@)", genresArray];
NSArray *filterArray = [Yourarray filteredArrayUsingPredicate:predicate];
NSLog(@"filter: %@",filterArray);
针对以上场景试试这个:
NSDictionary *yourJson;
NSMutableArray *array1 = [[yourJson valueForKey:@"results"] mutableCopy];
NSMutableArray *array2 = [[yourJson valueForKey:@"results2"] mutableCopy];
for(int i =0 ; i<array2.count ; i++)
{
NSString *objectFromarray2 = [array2 objectAtIndex:i];
for(int j =0 ; j<array1.count ; j++)
{
NSDictionary *objectFromarray1 = [array2 objectAtIndex:i];
if([[objectFromarray1 valueForKey:@"id"] isEqualToString:objectFromarray2])
{
[array2 removeObjectAtIndex:i];
[array2 insertObject:objectFromarray1 atIndex:i];
[array1 removeObject:objectFromarray1];
}
}
}
是的,您可以使用 NSPredicate
来做到这一点,但为此您首先必须将所有字典对象存储到一个 NSArray
或 NSMutableArray
中,无论什么适合您的要求。
喜欢:
NSString *search1 = @"3";
NSString *search2 = @"1";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY SELF == %@) OR (ANY SELF == %@)", search1,search2];
NSArray *filtered = [Yourarray filteredArrayUsingPredicate:predicate];
Swift 中的一种方法 - 3 使用过滤器和映射
let resultArray = results["results"]!
let genArray = genres["genres"]!
var result1Array : [[String:Any]] = resultArray
var result2Array : [[String:Any]] = []
for gen in genArray {
if let elm = resultArray.filter({ [=10=]["id"] as! Int == gen }).first {
result2Array.append(elm)
result1Array = result1Array.filter({ [=10=]["id"] as! Int != elm["id"] as! Int })
}
}
print(["result2":result2Array])
print("\n")
print(["result1":result1Array])
下面是我的 JSON 回复的一部分,其中“results
”是一个字典数组:
{
"results": [
{
"id": 6,
"genre_name": "Action",
"cover_image": "http://54.254.204.81/images/Action.png"
},
{
"id": 5,
"genre_name": "Adventure",
"cover_image": "http://54.254.204.81/images/Adventure.png"
},
{
"id": 4,
"genre_name": "Romance",
"cover_image": "http://54.254.204.81/images/Romance.png"
},
{
"id": 3,
"genre_name": "Sci-Fci",
"cover_image": "http://54.254.204.81/images/Sci-Fi.png"
},
{
"id": 1,
"genre_name": "Guide",
"cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
},
{
"id": 2,
"genre_name": "Horror",
"cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
},
{
"id": 7,
"genre_name": "Emotional",
"cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
},
{
"id": 8,
"genre_name": "abcd",
"cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
}
]
}
现在我有另一个 JSON 响应,其中“genres
”是一个数组,其中包含的对象是具有键 'id' 的“results
”数组对象的子集。
{
"genres": [
3,
1
]
}
现在,我是否可以将“results
”拆分为两个数组“results1
”和“results2
”,例如:
{
"results1": [
{
"id": 6,
"genre_name": "Action",
"cover_image": "http://54.254.204.81/images/Action.png"
},
{
"id": 5,
"genre_name": "Adventure",
"cover_image": "http://54.254.204.81/images/Adventure.png"
},
{
"id": 4,
"genre_name": "Romance",
"cover_image": "http://54.254.204.81/images/Romance.png"
},
{
"id": 2,
"genre_name": "Horror",
"cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
},
{
"id": 7,
"genre_name": "Emotional",
"cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
},
{
"id": 8,
"genre_name": "abcd",
"cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
}
]
}
和
{
"results2": [
{
"id": 3,
"genre_name": "Sci-Fci",
"cover_image": "http://54.254.204.81/images/Sci-Fi.png"
},
{
"id": 1,
"genre_name": "Guide",
"cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
}
]
}
为此,您需要像这样将 NSPredicate
与 IN
和 NOT
一起使用。
首先根据您的两个 JSON 回复创建 resultArray
和 genresArray
。
NSArray *resultArray = [firstJSONResponse objectForKey:@"results"];
NSArray *genresArray = [secondJSONResponse objectForKey:@"genres"];
现在使用 NSPredicate
过滤 resultArray
以获得结果。
对于结果 1
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (id IN %@)",genresArray];
NSArray *firstSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",firstSplitArray);
对于结果 2
predicate = [NSPredicate predicateWithFormat:@"id IN %@",genresArray];
NSArray *secondSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",secondSplitArray);
喜欢以下方式:
NSMutableArray * genresArray = [NSMutableArray new];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (id IN %@)", genresArray];
NSArray *filterArray = [Yourarray filteredArrayUsingPredicate:predicate];
NSLog(@"filter: %@",filterArray);
针对以上场景试试这个:
NSDictionary *yourJson;
NSMutableArray *array1 = [[yourJson valueForKey:@"results"] mutableCopy];
NSMutableArray *array2 = [[yourJson valueForKey:@"results2"] mutableCopy];
for(int i =0 ; i<array2.count ; i++)
{
NSString *objectFromarray2 = [array2 objectAtIndex:i];
for(int j =0 ; j<array1.count ; j++)
{
NSDictionary *objectFromarray1 = [array2 objectAtIndex:i];
if([[objectFromarray1 valueForKey:@"id"] isEqualToString:objectFromarray2])
{
[array2 removeObjectAtIndex:i];
[array2 insertObject:objectFromarray1 atIndex:i];
[array1 removeObject:objectFromarray1];
}
}
}
是的,您可以使用 NSPredicate
来做到这一点,但为此您首先必须将所有字典对象存储到一个 NSArray
或 NSMutableArray
中,无论什么适合您的要求。
喜欢:
NSString *search1 = @"3";
NSString *search2 = @"1";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY SELF == %@) OR (ANY SELF == %@)", search1,search2];
NSArray *filtered = [Yourarray filteredArrayUsingPredicate:predicate];
Swift 中的一种方法 - 3 使用过滤器和映射
let resultArray = results["results"]!
let genArray = genres["genres"]!
var result1Array : [[String:Any]] = resultArray
var result2Array : [[String:Any]] = []
for gen in genArray {
if let elm = resultArray.filter({ [=10=]["id"] as! Int == gen }).first {
result2Array.append(elm)
result1Array = result1Array.filter({ [=10=]["id"] as! Int != elm["id"] as! Int })
}
}
print(["result2":result2Array])
print("\n")
print(["result1":result1Array])