I have an array of custom object name finalBarListArray
with entity BarCodeSKULists
(refer to the attachment 1) which contains sales_sub_category_name (string type)
, count (int type)
and an array of another custom object name arrayBarCodeSKUList (BarCodeSKUList)
(refer to the attachment 2/3/4).
Please see the below screenshots:
Now I need to create an array and have to check if there will be same sales_sub_category_name
value then need to add their arrayBarCodeSKUList
in one.
Like in above array finalBarListArray
, there are three elements with sales_sub_category_name 'ENVY 17', 'ENVY 15' & 'ENVY 15'
, so I need to merge second and third element into one for second index of array. It means Output should have only two elements, where first element should have ( count = 1
& one arrayBarCodeSKUList
) and the second element should have ( count = 2
& two arrayBarCodeSKUList
).
Use the following method to merge the array of custom objects, which has same sales_sub_category_names
.
- (NSArray *)mergeDuplicate {
NSMutableDictionary *mergedDictionary = [[NSMutableDictionary alloc]init];
// Use sales_sub_category_name value as a key for the dictioanry.
[finalBarListArray enumerateObjectsUsingBlock:^(BarCodeSKULists * _Nonnull object, NSUInteger idx, BOOL * _Nonnull stop) {
id existingItem = [mergedDictionary valueForKey:object.sales_sub_category_name];
if (existingItem) {
// If object exist then check that type is NSMutableArray or not
if ([existingItem isKindOfClass:[NSMutableArray class]]) {
// If yes then append with existing array
[existingItem addObject:object];
mergedDictionary[object.sales_sub_category_name] = existingItem;
} else if ([existingItem isKindOfClass:[BarCodeSKULists class]]) {
// Else if the object is `BarCodeSKULists ` class then create array and added previous item and current item into one array
NSMutableArray *itemList = [NSMutableArray arrayWithObjects:existingItem, object, nil];
mergedDictionary[object.sales_sub_category_name] = itemList;
}
} else {
// If it is first time then add it to the dictionary
mergedDictionary[object.sales_sub_category_name] = object;
}
}];
NSLog(@"%@", mergedDictionary.allValues);
return mergedDictionary.allValues;
}
mergedDictionary.allValues
will give the expected array of items
Update :
As per the discussion.
- (NSArray *)mergeDuplicate:(NSMutableArray *) list{
NSMutableDictionary *mergedDictionary = [[NSMutableDictionary alloc]init];
// Use sales_sub_category_name value as a key for the dictioanry.
[list enumerateObjectsUsingBlock:^(BarCodeSKUList * _Nonnull object, NSUInteger idx, BOOL * _Nonnull stop) {
BarCodeSKUList *existingItem = [mergedDictionary valueForKey:object.sales_sub_category_name];
if (existingItem) {
[existingItem.arrayBarCodeSKUList addObjectsFromArray:object.arrayBarCodeSKUList];
mergedDictionary[object.sales_sub_category_name] = existingItem;
} else {
// If it is first time then add it to the dictionary
mergedDictionary[object.sales_sub_category_name] = object;
}
}];
return mergedDictionary.allValues;
}
- (NSArray *)mergeObject{
NSMutableDictionary *dic = [NSMutableDictionary new];
[_originArray enumerateObjectsUsingBlock:^(BarCodeSKULists* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
id item = [dic objectForKey:obj.sales_sub_category_name];
if (item) {
BarCodeSKULists *list = (BarCodeSKULists *)item;
[list.arrayBarCodeSKUList addObject:obj.BarCodeSKUList];
list.count = (int)list.arrayBarCodeSKUList.count;
dic[obj.sales_sub_category_name] = list;
}
else{
dic[obj.sales_sub_category_name] = obj;
}
}];
return dic.allValues;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.