[英]Combine an array of objects for the same value in Objective-C iOS
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).我有一个自定义对象名称finalBarListArray
数组,其中包含实体BarCodeSKULists
(请参阅附件 1),其中包含sales_sub_category_name (string type)
、 count (int type)
和另一个自定义对象名称arrayBarCodeSKUList (BarCodeSKUList)
的数组(请参阅附件 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.现在我需要创建一个数组,并且必须检查是否会有相同的sales_sub_category_name
值,然后需要将它们的arrayBarCodeSKUList
添加到一个中。
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.就像上面的数组finalBarListArray
一样, finalBarListArray
有sales_sub_category_name 'ENVY 17', 'ENVY 15' & 'ENVY 15'
三个元素,所以我需要将第二个和第三个元素合并为一个数组的第二个索引。 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
).这意味着输出应该只有两个元素,其中第一个元素应该有( count = 1
& one arrayBarCodeSKUList
),第二个元素应该有( count = 2
& two arrayBarCodeSKUList
)。
Use the following method to merge the array of custom objects, which has same sales_sub_category_names
.使用以下方法合并具有相同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 mergedDictionary.allValues
将给出预期的项目数组
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.