ios - Compare two arrays of dictionaries and add/remove items -
i have 2 arrays of dictionaries this
arrcurrent = [{ id = 1; name = "name1"; },{ id = 2; name = "name2"; },{ id = 3; name = "name3"; }]; arrupdated = [{ id = 1; name = "name1 has changed"; },{ id = 2; name = "name2 has changed"; },{ id = 4; name = "name4 new item"; }];
my requirement merge these 2 arrays according following conditions
1) if arrupdated contains new items(item new id) should added new item.
2) if arrupdated contains items same id of arrcurrent contains, items should replaced updated items
so, final array should this
arrfinal = [{ id = 1; name = "name1 has changed"; },{ id = 2; name = "name2 has changed"; },{ id = 3; name = "name3"; },{ id = 4; name = "name4 new item"; }];
hopes requirement clear, whats best way this?
this how trying it. methods duplicate items. also, looping through arrays not best way
please note: actual code contains different names above mentioned array names. same logic
arrsavedsectors = arrcurrent
arrallupdatedsectors = arrupdated
arrfilteredsectors = arrfinal
nsmutablearray *arrsavedsectors = [[nsmutablearray alloc]initwitharray: [dictionary objectforkey:@"arrsectors"]]; nsmutablearray *arrfilteredsectors = [[nsmutablearray alloc]initwitharray:arrsavedsectors]; // add updated sectors list nsarray *arrallupdatedsectors = [nsarray arraywitharray:[sectordetails objectforkey:@"allupdatedsectors"]]; if([arrsavedsectors count] > 0){ // check updated sectors (which saved in plist, updated details) for(nsdictionary *dicsector in arrsavedsectors){ for(nsdictionary *dicupdatedsector in arrallupdatedsectors){ if([[dicupdatedsector objectforkey:@"id"] isequaltostring:[dicsector objectforkey:@"id"]]){ [arrfilteredsectors removeobject:dicsector]; [arrfilteredsectors addobject:dicupdatedsector]; } else{ [arrfilteredsectors addobject:dicupdatedsector]; } } } } else{ [arrfilteredsectors addobjectsfromarray:arrallupdatedsectors]; }
i think loop this.
for(int x=0;x<[arrupdate count];x++){ //condition 2 if([[arrcurrent valueforkey:@"id"] containsobject:[[arrupdate objectatindex:x] valueforkey:@"id"]]){ } //condition 1 else{ [arrcurrent addobject: [arrupdate objectatindex:x]] } }
though use predicate.
i haven't tested code not have machine. @ least modify this.
update:
i ended finding machine prove code works. using test data asker provided:
nsmutablearray *arrcurrent = [nsmutablearray arraywitharray:@[@{ @"id":@"1", @"name":@"name1" },@{ @"id":@"2", @"name":@"name2" },@{ @"id":@"3", @"name":@"name3" }]]; nsmutablearray *arrupdated = [nsmutablearray arraywitharray:@[@{ @"id":@"1", @"name":@"name1 has changed" },@{ @"id":@"2", @"name":@"name2 has changed" },@{ @"id":@"4", @"name":@"name4 new item" }]]; for(int x=0;x<[arrupdated count];x++){ //condition 2 if([[arrcurrent valueforkey:@"id"] containsobject:[[arrupdated objectatindex:x] valueforkey:@"id"]]){ //start updating current array for(int y=0;y<[arrcurrent count];y++){ if([[[arrcurrent objectatindex:y] valueforkey:@"id"] isequaltostring:[[arrupdated objectatindex:x] valueforkey:@"id"]]){ nslog(@" (%@) change (%@)",[[arrcurrent objectatindex:y] valueforkey:@"name"],[[arrupdated objectatindex:x] valueforkey:@"name"]); [arrcurrent replaceobjectatindex:y withobject:[arrupdated objectatindex:x] ]; break; } } } //condition 1 else{ [arrcurrent addobject: [arrupdated objectatindex:x]]; } } nslog(@"result>: %@",arrcurrent);
result: >>
2015-06-04 23:24:30.949 dd[7523:60b] (name1) change (name1 has changed) 2015-06-04 23:24:30.950 dd[7523:60b] (name2) change (name2 has changed) 2015-06-04 23:24:30.951 dd[7523:60b] result>: ( { id = 1; name = "name1 has changed"; }, { id = 2; name = "name2 has changed"; }, { id = 3; name = name3; }, { id = 4; name = "name4 new item"; } )
Comments
Post a Comment