如何根据元素内部的值将nsdictionary数组分组 [英] how to group array of nsdictionary according to the value inside the element

查看:239
本文介绍了如何根据元素内部的值将nsdictionary数组分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典数组,需要根据作为元素的一部分的PO进行分组,并根据相同的PO得到quantityOrdered的总和。
PO是动态的,这意味着它可以是需要过滤的任何值,并相应地计算quantityOrderd。

请帮助。

  {
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;
},

我更新了示例并使其易于阅读。

解决方案

   - (NSArray *)whatever:(NSArray *)dictionaries 
{
NSMutableArray * results = [[NSMutableArray alloc] init];
NSMutableDictionary * resultsByPO = [[NSMutableDictionary alloc] init];

for(NSDictionary * dictionary in dictionaries){
id po = [dictionary objectForKey:@PO];
NSMutableDictionary * result = [resultsByPO objectForKey:po];

if(result == nil){
result = [[NSMutableDictionary alloc] init];
[resultsByPO setObject:result forKey:po];
[results addObject:result];
[result setObject:po forKey:@PO];
}

double total = [[result objectForKey:@TotalQuantityReceived] doubleValue];
total + = [[dictionary objectForKey:@QuantityOrdered] doubleValue];

int count = 1 + [[result objectForKey:@Count] intValue];

[result setObject:@(total)forKey:@TotalQuantityReceived];
[result setObject:@(count)forKey:@Count];
}
返回结果;



$ b更多的痛苦会随着PO值不符合 NSCopying


I have array of dictionary that needs to be grouped according to PO which is part of the element and also get the total of quantityOrdered according to the same PO. The PO is dynamic it means it can be any value that needs to be filtered and compute the quantityOrderd accordingly.

Please help.

{
    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;
},

Apology for the confusion or incomplete question but i need to create a new array of dictionary with similar like this :

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

i updated my example and make it easy to read.

解决方案

- (NSArray *)whatever:(NSArray *)dictionaries
{
    NSMutableArray *results = [[NSMutableArray alloc] init];
    NSMutableDictionary *resultsByPO = [[NSMutableDictionary alloc] init];

    for (NSDictionary *dictionary in dictionaries) {
        id po = [dictionary objectForKey:@"PO"];
        NSMutableDictionary *result = [resultsByPO objectForKey:po];

        if (result == nil) {
            result = [[NSMutableDictionary alloc] init];
            [resultsByPO setObject:result forKey:po];
            [results addObject:result];
            [result setObject:po forKey:@"PO"];
        }

        double total = [[result objectForKey:@"TotalQuantityReceived"] doubleValue];
        total += [[dictionary objectForKey:@"QuantityOrdered"] doubleValue];

        int count = 1 + [[result objectForKey:@"Count"] intValue];

        [result setObject:@(total) forKey:@"TotalQuantityReceived"];
        [result setObject:@(count) forKey:@"Count"];
    }
    return results;
}

More pain will come with PO values not conforming to NSCopying.

这篇关于如何根据元素内部的值将nsdictionary数组分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆