Merging an array of objects for the same value in Objective-C iOS

I have an array of user names for finalBarListArray objects with the entity "BarCodeSKULists" (see attachment 1) that contains "sales_sub_category_name (string type)", count (type int) and an array of another user name 'arrayBarCodeSKUList (BarCodeSKUList)' (see attachment 2/3/4).

See screenshots below:

enter image description here

enter image description here

enter image description here

enter image description here

Now I need to create an array and check if the same value is "sales_sub_category_name", and then add their "arrayBarCodeSKUList" to one.

finalBarListArray, sales_sub_category_name 'ENVY 17', 'ENVY 15' 'ENVY 15', . , Output , (count = 1 arrayBarCodeSKUList), (count = 2 two arrayBarCodeSKUList).

, . !

+4
2

, 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
- (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;

}

+1

Source: https://habr.com/ru/post/1682108/


All Articles