Copy from dictionary array to dictionary array

I am collecting identifiers in an array from an array of dictionaries like this

NSArray *places=   @[ 
    {place_id = @"3dsfdDfGDH";      
    place_url = @"/Peru/Lambayeque/Chiclayo";      
    place_name = @"Peru";}
     ,
    {place_id = @"HUHiKcZVU7ItMyQ";       
    place_url = @"/Peru/La+Libertad/Huanchaco";      
    place_name = @"Peru";}
    ,
    {place_id = @"7JL1K5FVUbg0vg";        
    place_url = @"/United+Kingdom/England/Buckley+Hill";      
    place_name = @"United Kingdom";}
                   ];

NSArray *placeIds= [places valueForKeyPath:place_id]; 

It works fine, but what I want is a different array with place_id as well as place_url but NOT Name. sort of

I want NSArray * PlaceIdAndURL to have dictionaries like below

                   @[
         {place_id = @"3dsfdDfGDH";     
        place_url = @"/Peru/Lambayeque/Chiclayo";}
         ,
        {place_id = @"HUHiKcZVU7ItMyQ";       
        place_url = @"/Peru/La+Libertad/Huanchaco";}
        ,
        {place_id = @"7JL1K5FVUbg0vg";        
        place_url = @"/United+Kingdom/England/Buckley+Hill";}
                       ];

How can I get without a loop an entire array, like the one I had first with ValueForKeyPathabove

0
source share
1 answer

I do not think that this is possible in one liner that will not be repeated ... but here is the code that will do the job. In fact, you can do this logic in a method and add as your category in NSArray and NSMutableArray ...

places_dup will contain the dictionaries you are looking for.

NSArray *places=   @[
                     @{@"place_id" : @"NFxmX.VVU7LtQwQ",
                       @"place_url" : @"/Peru/Lambayeque/Chiclayo",
                       @"place_name" : @"dadfasdf"}
                     ,
                     @{@"place_id" : @"HUHiKcZVU7ItMyQ",
                       @"place_url" : @"/Peru/La+Libertad/Huanchaco",
                       @"place_name" : @"dadfasdf"}
                     ,
                     @{@"place_id" : @"7JL1K5FVUbg0vg",
                       @"place_url" : @"/United+Kingdom/England/Buckley+Hill",
                       @"place_name" : @"dadfasdf"}
                     ];
NSMutableArray *places_dup = [@[] mutableCopy];
[places enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) {
    NSDictionary *dictionary = (NSDictionary *) item;
    NSMutableDictionary *filteredDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
    [filteredDictionary removeObjectForKey:@"place_name"];
    [places_dup addObject:filteredDictionary];
}];
+1

All Articles