【问题标题】:Sum objectAtIndex values in NSArray of NSArray OR Add value with existing value for key in NSDictionary将 NSArray 的 NSArray 中的 objectAtIndex 值相加或将值与 NSDictionary 中的键的现有值相加
【发布时间】:2015-04-03 13:07:09
【问题描述】:

我需要的任何一种解决方案。建议我最好的答案。

我有数百个这样的数组给我最好的解决方案。我不喜欢嵌套 ForLoops

1。我需要通过 ObjectAtIndex : 0 Distinct Union the Array,并且还需要在 ObjectAtIndex : 1 中求和它们的值。

A--> [[1,"100.0"],[2,"100.0"],[2,"100.0"],[3,"100.0"],[4,"100.0"],[3 ,"100.0"],[4,"100.0"]]

B--> [[1,250],[2,250],[1,250],[3,"200.2"],[1,"200.2"],[4,"200.2"],[1,"200.2" ],[4,"200.2"]]

我需要这样的A--> [[1,"100.0"],[2,"200.0"],[3,"200.0"],[4,"200.0"]]

2。我必须在 Dictionary 中设置对象 A 和 B 数组。但如果键存在于字典中,则将该值与现有值相加。

{

 A =     {
    1 = 100;
    2 = 100;
    3 = 100;
    4 = 100;
};

 B =     {
    1 = 250;
    2 = 250;
    3 = "200.2";
    4 = "200.2";
};

}

喜欢

NSArray* uniqueValues = [data valueForKeyPath:[NSString stringWithFormat:@"@distinctUnionOfObjects.%@", @"self"]];

【问题讨论】:

    标签: ios objective-c arrays nsdictionary


    【解决方案1】:
    NSArray* A = @[@[@1, @100.0],@[@2, @100.0],@[@3, @100.0],@[@1, @100.0],@[@1, @100.0]];
    
    NSMutableDictionary* sum = [NSMutableDictionary new];
    for (NSArray* item in A)
    {
        id key = item[0];
        if (sum[key] == nil)
        {
            sum[key] = item[1];
        }
        else
        {
            sum[key] = @([sum[key] floatValue] + [item[1] floatValue]);
        }
    }
    
    NSLog(@"%@", sum.description);
    

    输出:

    3 = 100;
    1 = 300;
    2 = 100;
    

    【讨论】:

    • 谢谢...我没有像您处理的那样使用字典...建议我使用棘手代码的教程...
    【解决方案2】:

    基于 KVC 集合运算符的替代解决方案。

        NSArray* arrayOFArray = @[@[@1, @100.0],@[@2, @100.0],@[@3, @100.0],@[@1, @100.0],@[@1, @100.0]];
    
        NSMutableDictionary* sumDic = [NSMutableDictionary new];
        NSInteger index = 0; // By changing index position. You get unique sum either way.
        for (NSArray* item in arrayOFArray)
        {
           id key = item[index];
            if (key && sumDic[key] == nil) {
    
                NSArray *keyArray = [self findSameValueKey:arrayOFArray WithKey:key withIndex:index];
                NSArray *unionArray = [keyArray valueForKeyPath: @"@unionOfArrays.self"];
                NSNumber *sum = [unionArray valueForKeyPath:@"@sum.floatValue"];
                sumDic[key] = [NSNumber numberWithInt:[sum floatValue] - ([keyArray count]* [key floatValue])];
            }
        }
    
        NSLog(@"%@", sumDic.description);
    
    
        // Helps to find same value key.
        -(NSArray*)findSameValueKey:(NSArray*)dateArray WithKey:(NSString*)key withIndex:(NSInteger)index {
            NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSArray *resultsArray, NSDictionary *bind){
                if ([resultsArray[index] isEqual:key]) {
                    return true;
                }
                return false;
            }];
    
            // Apply the predicate block .
            NSArray *sameValueKeys = [dateArray filteredArrayUsingPredicate:predicate];
            return sameValueKeys;
        }
    

    【讨论】:

      猜你喜欢
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多