Ankur Arya
Ankur Arya

Reputation: 4723

Merging NSArrays containing NSDictionarys

I have an NSMutableArray of NSDictionarys something like this:

myArray (
    {
    chatins = 20;
    placeImageString = 244211112265925;
},
    {
    chatins = 5;
    placeImageString = 154909144575109;
},
    {
    chatins = 30;
    placeImageString = 193867280641162;
},
    {
    chatins = 13;
    placeImageString = 224627130902;
},

)

and another NSMutableArray of NSDictionarys something like this:

myArray2 
(
    {
    category = "Local business";
    distance = "0.1";
    name = "Mts India";
    placeImageString = 244211112265925;
},
    {
    category = "Local business";
    distance = "0.17";
    name = "Aegis Ltd";
    placeImageString = 154909144575109;
},
    {
    category = "Automobiles and parts";
    distance = "0.19";
    name = Autopsyche;
    placeImageString = 78480207511;
},
    {
    category = Company;
    distance = "0.19";
    name = "The Oberoi, Gurgaon";
    placeImageString = 121676041233945;
},

)

I want to merge myArray and myArray2 in order to get resulted NSMutableArray of NSDictionarys something like below where my placeImageString is the key to match the data in both arrays of dictionaries and if the key not found in myArray2 then the value of chatins key should be 0.

myArray3 
(
{
category = "Local business";
distance = "0.1";
name = "Mts India";
placeImageString = 244211112265925;
 chatins = 20;

},
{
category = "Local business";
distance = "0.17";
name = "Aegis Ltd";
placeImageString = 154909144575109;
chatins = 5;

},
{
category = "Automobiles and parts";
distance = "0.19";
name = Autopsyche;
placeImageString = 78480207511;
chatins = 0;


},
    {
    category = Company;
    distance = "0.19";
    name = "The Oberoi, Gurgaon";
    placeImageString = 121676041233945;
    chatins = 0;

    },
)

Upvotes: 2

Views: 96

Answers (2)

Mrunal
Mrunal

Reputation: 14118

Here I have a sample code for you:

  NSMutableArray *array1 = [[NSMutableArray alloc] init];
  NSMutableArray *array2 = [[NSMutableArray alloc] init];


  NSMutableDictionary * dict = [[NSMutableDictionary alloc]
                                initWithObjects:[NSArray arrayWithObjects:@"1",@"ABC", nil]
                                forKeys:[NSArray arrayWithObjects:@"ID",@"NAME", nil]];
  [array1 addObject:dict];

  dict = nil;

  /*
   Same way added 2 more dictionaries to the same array - array1
   */

  NSLog(@"array1: %@", array1);


  dict = nil;

  dict = [[NSMutableDictionary alloc]
          initWithObjects:[NSArray arrayWithObjects:@"1",@"DEF", nil]
          forKeys:[NSArray arrayWithObjects:@"ID",@"ADDRESS", nil]];

  [array2 addObject:dict];

  dict = nil;

  /*
   Same way added 2 more dictionaries to the same array - array2
   */


  NSLog(@"array2: %@", array2);


  for (int index = 0; index < [array1 count]; index ++) {

    NSMutableDictionary *dict1 = [array1 objectAtIndex:index];

    for (NSMutableDictionary *dict2 in array2) {

      if ([[dict1 objectForKey:@"ID"] isEqualToString:
           [dict2 objectForKey:@"ID"]]) {

        [dict1 setObject:[dict2 objectForKey:@"ADDRESS"] forKey:@"ADDRESS"];
      }
    }
  }

Now at the end if you check with array1, there is one more key (ADDRESS) added in all those dictionary.

Hope this help.

-Mrunal

Upvotes: 2

trumpetlicks
trumpetlicks

Reputation: 7065

1. Create a new empty NSMutableArray newArray
2. Loop through all NSDictionaries of myArray2

    a. Copy the current NSDictionary to a new NSMutableDictionary newDict
    b. Loop through myArray1
        b1. if myArray1.dict.placeImageString == newDict.placeImageString
            - Add myArray1.dict.chatins to newDict
            - break out of loop
    c. add newDict to newArray

Upvotes: 0

Related Questions