【问题标题】:creating a dictionary summary base on array of nsdictionary基于 nsdictionary 数组创建字典摘要
【发布时间】:2014-02-02 11:20:22
【问题描述】:

我有一个字典数组,需要根据里面的值进行汇总。 PO 行将是动态的分组标准。

需要统计每个采购订单的元素数量和接收数量的总和。

 {
    PO = PO2;
    QuantityReceived = 1;
},
{
    PO = PO1;
    QuantityReceived = 3;
},
{

    PO = PO1;
    QuantityReceived = 3;
},
{
    PO = PO3;
    QuantityReceived = 2;
},
{
    PO = PO2;
    QuantityReceived = 2;
},
{
    PO = PO3;
    QuantityReceived = 4;
},
{

    PO = PO1;
    QuantityReceived = 1;
},

样本输出为:

{
   PO = PO1;
   TotalQuanityReceived=7;
   LineItems=3;
},
{
   PO = PO2;
   TotalQuanityReceived=3;
   LineItems=2;
},
{
   PO = PO3;
   TotalQuanityReceived=6;
   LineItems=2;
},

【问题讨论】:

标签: ios objective-c nsarray nsdictionary nspredicate


【解决方案1】:

简而言之:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {
        //INPUT DATA
        NSArray *inputPos = @[ @{ @"PO":@"PO2", @"QuantityReceived":@1 },
        @{ @"PO":@"PO1", @"QuantityReceived":@3 },
        @{ @"PO":@"PO1", @"QuantityReceived":@3 },
        @{ @"PO":@"PO3", @"QuantityReceived":@2 },
        @{ @"PO":@"PO2", @"QuantityReceived":@2 },
        @{ @"PO":@"PO3", @"QuantityReceived":@4 },
        @{ @"PO":@"PO1", @"QuantityReceived":@1 }];

        //REAL LOGIC
        NSMutableArray *originalPOs = [inputPos mutableCopy];
        NSMutableArray *groupedPOs = [@[] mutableCopy];

        while(originalPOs.count) {
            NSArray *matchingPOs = [originalPOs filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"PO == %@", originalPOs[0] [@"PO"]]];
            [originalPOs removeObjectsInArray:matchingPOs];

            NSInteger sum = 0;
            NSArray *qs = [matchingPOs valueForKeyPath:@"QuantityReceived"];
            for(NSNumber *quantity in qs)
                sum += quantity.integerValue;

            [groupedPOs addObject:@{@"PO":matchingPOs[0][@"PO"], @"QuantityReceived":@(sum), @"LineItems":@(qs.count)}];
        }

        NSLog(@"%@", groupedPOs);
    }
}

【讨论】:

  • 包裹在可运行程序中,真正的逻辑很短
【解决方案2】:

可能有几种方法。可以这样写:

    NSString *mainKey = @"PO";
    NSString *totalKey = @"TotalQuantityReceived";
    NSString *quantityKey = @"QuantityReceived";
    NSString *lineItemsKey = @"LineItems";

    void (^updateDictionary)(NSMutableDictionary *, NSDictionary *) = ^(NSMutableDictionary *outputDictionary, NSDictionary *inputDictionary) {
        outputDictionary[totalKey] = @([outputDictionary[totalKey] integerValue] + [inputDictionary[quantityKey] integerValue]);
        outputDictionary[lineItemsKey] = @([outputDictionary[lineItemsKey] integerValue] + 1);
    };

     NSMutableArray *output = [NSMutableArray array];
    [array enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger idx, BOOL *stop) {
        id key = dictionary[mainKey];
        NSDictionary *existingDictionary = [[output filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K == %@", mainKey, key]] lastObject];
        NSMutableDictionary *outputDictionary = [existingDictionary mutableCopy];
        if (!outputDictionary) {
            outputDictionary = [NSMutableDictionary dictionaryWithObject:key forKey:mainKey];
            updateDictionary(outputDictionary, dictionary);
            [output addObject:outputDictionary];
        } else {
            updateDictionary(outputDictionary, dictionary);
            [output replaceObjectAtIndex:[output indexOfObject:existingDictionary] withObject:outputDictionary];
        }
    }];

    [output sortUsingComparator:^NSComparisonResult(NSDictionary *dictionary1, NSDictionary *dictionary2) {
        NSString *key1 = dictionary1[mainKey];
        NSString *key2 = dictionary2[mainKey];
        return [key1 compare:key2];
    }];

【讨论】:

  • 谢谢你 Maciej Oczko 我需要先学习你的代码,这让我很惊讶。我当然是新手 :)
  • 这看起来有点复杂......虽然我确信它会起作用。有太多的排序正在进行:D
  • Daij-Djan 你是对的。代码已开放优化;)您的解决方案更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
  • 2021-07-20
  • 2019-11-17
  • 2017-01-01
  • 1970-01-01
相关资源
最近更新 更多