1

我有以下数组,我有一个包含多个字典的数组,我需要获取相同 prod_type 的字典并创建另一个具有唯一键的数组

nsarray
  {
    {
      prod_type=abc;
      fund=100;
     };
        {
      prod_type=abc;
      fund=100;
     };
   {
      prod_type=abc;
      fund=100;
     };
   {
      prod_type=pqr;
      fund=100;
     };
   {
      prod_type=pqr;
      fund=100;
     };
   {
      prod_type=xyz;
      fund=100;
     };
   {
      prod_type=xyz;
      fund=100;
     };

我需要从上面的数组中遵循数组格式

 nsarray=
  {
     abc=
     {
     {
      prod_type=abc;
      fund=100;
     };
        {
      prod_type=abc;
      fund=100;
     };
   {
      prod_type=abc;
      fund=100;
     };
      }

   pqr=
      {
       {
      prod_type=pqr;
      fund=100;
     };
   {
      prod_type=pqr;
      fund=100;
     };
    }
 xyz=
     {
       {
      prod_type=xyz;
      fund=100;
     };
   {
      prod_type=xyz;
      fund=100;
     };

      }

   }
4

3 回答 3

2

使用NSPredicate获得理想的结果。

NSString *selectedCategory=@"abc";

//filter array by category using predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"prod_type == %@", selectedCategory];

NSArray *filteredArray = [yourAry filteredArrayUsingPredicate:predicate];

NSDictionary *abcDic = [NSDictionary dictionaryWithObject:filteredArray forKey:@"abc"];

[yourNewAry addObject:abcDic];

你可以重复它为其他

这里有一个很好的解释它的谓词

于 2013-07-20T07:35:40.717 回答
2

如果您想要一个完全自动化的解决方案(无需重新指定每个prod_type),请使用此代码:

NSMutableArray *keys = [originalArray mutableArrayValueForKey:@"prod_type"];
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:keys];
NSArray *uniqueKeys = orderedSet.array;

NSMutableArray *resultArray = [[NSMutableArray alloc] init];

for(NSString *key in uniqueKeys){
    NSPredicate *keyPredicate = [NSPredicate predicateWithFormat:@"prod_type = %@",key];
    NSDictionary *keyDictionary = [NSDictionary dictionaryWithObject:[originalArray filteredArrayUsingPredicate:keyPredicate] forKey:key];
    [resultArray addObject:keyDictionary];
}

NSLog(@"%@",resultArray);
于 2013-07-20T08:17:25.183 回答
0

试试这样

    NSMutableDictionary *resultdict = [[NSMutableDictionary alloc]init];
    NSMutableArray *keysArray =[array mutableArrayValueForKey:@"prod_type"];//here you'l get all the prod_type values in an array
      for(int i=0;i<[keysArray count];i++){
          NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"prod_type CONTAINS %@",[keysArray objectAtIndex:i]];
          NSArray* searchResults=[array  filteredArrayUsingPredicate:resultPredicate];
          [resultdict setObject:searchResults forKey:[keysArray objectAtIndex:i]];
    }
    NSLog(@"%@",resultdict);

前任:-

NSMutableArray *array =[[NSMutableArray alloc]init];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithObjects:@[@"abc",@"100"] forKeys:@[@"name",@"value"]];
    NSMutableDictionary *dict1 = [[NSMutableDictionary alloc]initWithObjects:@[@"pqr",@"100"] forKeys:@[@"name",@"value"]];
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]initWithObjects:@[@"pqr",@"100"] forKeys:@[@"name",@"value"]];

    [array addObject:dict];
    [array addObject:dict1];
    [array addObject:dict2];

NSLog(@"%@",array);

(
        {
        name = abc;
        value = 100;
    },
        {
        name = pqr;
        value = 100;
    },
        {
        name = pqr;
        value = 100;
    }
)


NSMutableDictionary *resultdict = [[NSMutableDictionary alloc]init];
    NSMutableArray *keysArray =[array mutableArrayValueForKey:@"name"];
      for(int i=0;i<[keysArray count];i++){
          NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"name CONTAINS %@",[keysArray objectAtIndex:i]];
          NSArray* searchResults=[array  filteredArrayUsingPredicate:resultPredicate];
          [resultdict setObject:searchResults forKey:[keysArray objectAtIndex:i]];
    }

NSLog(@"%@",resultdict);

{
    abc =     (
                {
            name = abc;
            value = 100;
        }
    );
    pqr =     (
                {
            name = pqr;
            value = 100;
        },
                {
            name = pqr;
            value = 100;
        }
    );
}
于 2013-07-20T07:49:01.153 回答