14

NSArray があり、配列内の各オブジェクトには groupId と名前があります。各オブジェクトは一意ですが、同じ groupId を持つオブジェクトが多数あります。名前が対応する groubId を持つ単一のオブジェクトにグループ化されるように、配列をバラバラにして再構築する方法はありますか? 現在の配列は次のようになります。

2013-03-12 20:50:05.572 appName[4102:702] the  array:  (
        {
        groupId = 1;
        name = "Dan";
    },
        {
        groupId = 1;
        name = "Matt";
    },
        {
        groupId = 2;
        name = "Steve";
    },
        {
        groupId = 2;
        name = "Mike";
    },
        {
        groupId = 3;
        name = "John";

    },
        {
        groupId = 4;
        name = "Kevin";
    }
)

これは私がそれをどのように見せたいかです:

2013-03-12 20:50:05.572 appName[4102:702] the  array:  (
        {
        groupId = 1;
        name1 = "Dan";
        name2 = "Matt";
    },
        {
        groupId = 2;
        name1 = "Steve";
        name2 = "Mike";
    },
        {
        groupId = 3;
        name = "John";

    },
        {
        groupId = 4;
        name = "Kevin";
    }
)

編集:私は多くの試みで失敗しましたが、ほとんどがこのようなものに沿っています(ずさんなレクリエーションですが、アイデアを与えるために):

int idNum = 0;
for (NSDictionary *arrObj in tempArr){
    NSString *check1 = [NSString stringWithFormat:@"%@",[arrObj valueForKey:@"groupId"]];
    NSString *check2 = [NSString stringWithFormat:@"%@",[[newDict valueForKey:@"groupId"]];
    if (check1 == check2){
        NSString *nameStr = [NSString stringWithFormat:@"name_%d",idNum];
        [newDict setValue:[arrObj valueForKey:@"name"] forKey:nameStr];
    }
    else {
        [newDict setValue:arrObj forKey:@"object"];
    }
    idNum++;
}  
4

5 に答える 5

34
NSArray *array = @[@{@"groupId" : @"1", @"name" : @"matt"},
                   @{@"groupId" : @"2", @"name" : @"john"},
                   @{@"groupId" : @"3", @"name" : @"steve"},
                   @{@"groupId" : @"4", @"name" : @"alice"},
                   @{@"groupId" : @"1", @"name" : @"bill"},
                   @{@"groupId" : @"2", @"name" : @"bob"},
                   @{@"groupId" : @"3", @"name" : @"jack"},
                   @{@"groupId" : @"4", @"name" : @"dan"},
                   @{@"groupId" : @"1", @"name" : @"kevin"},
                   @{@"groupId" : @"2", @"name" : @"mike"},
                   @{@"groupId" : @"3", @"name" : @"daniel"},
                   ];

NSMutableArray *resultArray = [NSMutableArray new];
NSArray *groups = [array valueForKeyPath:@"@distinctUnionOfObjects.groupId"];
for (NSString *groupId in groups)
{
    NSMutableDictionary *entry = [NSMutableDictionary new];
    [entry setObject:groupId forKey:@"groupId"];

    NSArray *groupNames = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"groupId = %@", groupId]];
    for (int i = 0; i < groupNames.count; i++)
    {
        NSString *name = [[groupNames objectAtIndex:i] objectForKey:@"name"];
        [entry setObject:name forKey:[NSString stringWithFormat:@"name%d", i + 1]];
    }
    [resultArray addObject:entry];
}

NSLog(@"%@", resultArray);

出力:

    (
        {
        groupId = 3;
        name1 = steve;
        name2 = jack;
        name3 = daniel;
    },
        {
        groupId = 4;
        name1 = alice;
        name2 = dan;
    },
        {
        groupId = 1;
        name1 = matt;
        name2 = bill;
        name3 = kevin;
    },
        {
        groupId = 2;
        name1 = john;
        name2 = bob;
        name3 = mike;
    }
 )
于 2013-03-13T02:02:00.333 に答える
2

これは、NSArray の NSDictionary を呼び出します。すばやくエレガントな方法はありません。ソースをスクロールする必要があります。

NSMutableDictionary *d = [NSMutableDictionary dictionaryWithCapacity:10]; //Or use alloc/init
for(SomeObject o in appname) //What's the type of objects? you tell me
{
    NSObject *ID = [o objectForKey: @"groupId"];
    NSMutableArray *a = [d objectForKey: ID];
    if(a == nil)
    {
        a = [NSMutableArray arrayWithCapacity: 10];
        [d setObject:a forKey: ID];
    }
    [a addObject: [o objectForKey: @"name"]];
}

編集: キーのデータ型を想定しないように編集されました。

于 2013-03-13T01:47:35.690 に答える
0

以下のコードは、その配列内の各ディクショナリ内の一致するキーについてオブジェクトをグループ化することにより、NSArray を再構築します。

//only to a take unique keys. (key order should be maintained)
NSMutableArray *aMutableArray = [[NSMutableArray alloc]init];

NSMutableDictionary *dictFromArray = [NSMutableDictionary dictionary];

for (NSDictionary *eachDict in arrOriginal) {
  //Collecting all unique key in order of initial array
  NSString *eachKey = [eachDict objectForKey:@"roomType"];
  if (![aMutableArray containsObject:eachKey]) {
   [aMutableArray addObject:eachKey];
  }

  NSMutableArray *tmp = [grouped objectForKey:key];
  tmp  = [dictFromArray objectForKey:eachKey];

 if (!tmp) {
    tmp = [NSMutableArray array];
    [dictFromArray setObject:tmp forKey:eachKey];
 }
[tmp addObject:eachDict];

}

//NSLog(@"dictFromArray %@",dictFromArray);
//NSLog(@"Unique Keys :: %@",aMutableArray);

//再び辞書から配列に変換中...

self.finalArray = [[NSMutableArray alloc]init];
for (NSString *uniqueKey in aMutableArray) {
   NSDictionary *aUniqueKeyDict = @{@"groupKey":uniqueKey,@"featureValues":[dictFromArray objectForKey:uniqueKey]};
   [self.finalArray addObject:aUniqueKeyDict];
}

それが役立つことを願っています..

于 2016-07-20T05:01:18.543 に答える