我有一个自定义对象名称数组" finalBarListArray"与实体" BarCodeSKULists" (参见附件1)其中包含' sales_sub_category_name(字符串类型)',count(int类型)和另一个自定义对象名称的数组' arrayBarCodeSKUList(BarCodeSKUList)' (参见附件2/3/4)。
请参阅以下屏幕截图:
现在我需要创建一个数组,并且必须检查是否存在相同的' sales_sub_category_name'然后需要添加他们的' arrayBarCodeSKUList'在一个。
与上面的数组" finalBarListArray"一样,有三个元素,包括sales_sub_category_name' ENVY 17'' ENVY 15' &安培; ' ENVY 15',所以我需要将第二个和第三个元素合并为一个用于第二个数组索引。这意味着输出应该只有两个元素,第一个元素应该有(count = 1& one arrayBarCodeSKUList),第二个元素应该有(count = 2& two arrayBarCodeSKUList)。
请告诉我。谢谢!
答案 0 :(得分:1)
使用以下方法合并自定义对象数组,这些对象具有相同的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
会提供预期的项目数组
更新:
根据讨论。
- (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;
}
答案 1 :(得分:1)
- (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;
}