0

I have array with dictionaries like this:

Array: (
 {
customerUS= {
 DisplayName = "level";
 InternalName = "Number 2";
 NumberValue = 1;
 },
 customerCAN= {
 DisplayName = "PurchaseAmount";
 InternalName = "Number 1";
 NumberValue = 3500;
 };
}
)

I want to filter the dictionaries base on particular value and not the key. For example I want all the dictionaries with values on any key of 3500. Does any body knows how can I do this?

I'll really appreciate your help.

messivanio
  • 2,263
  • 18
  • 24
Juan
  • 627
  • 2
  • 9
  • 27
  • [I suspect the answer you seek can be found in this question](http://stackoverflow.com/questions/958622/using-nspredicate-to-filter-an-nsarray-based-on-nsdictionary-keys), but I'm not going to investigate too deeply enough to decide to mark your question as a duplicate. – Michael Dautermann Jul 24 '13 at 21:42
  • @MichaelDautermann in the link you have there is filtering by key and I don't want that or need that. I need to filter by value – Juan Jul 24 '13 at 21:49
  • http://stackoverflow.com/a/9509181/1224741 – QED Jul 25 '13 at 04:19
  • possible duplicate of [ios sorting array of dictionaries by key of inner dictionary](http://stackoverflow.com/questions/9509138/ios-sorting-array-of-dictionaries-by-key-of-inner-dictionary) – QED Jul 25 '13 at 04:20

2 Answers2

0

Try a predicate format like:

@"%@ IN SELF", testValue

When you filter the array each will be run against the IN. When you pass IN a dictionary it uses the values of the dictionary.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"%@ in self", @"3500"]; but doen't work. – Juan Jul 24 '13 at 22:17
  • Your question actually shows an array of dictionaries of dictionaries, is that accurate (it doesn't look like a log output)? – Wain Jul 24 '13 at 22:20
  • this is the out I'm getting – Juan Jul 24 '13 at 22:21
  • If you have nested dictionaries, what should the filter result be? And how many dictionaries are in the outer array? – Wain Jul 24 '13 at 22:23
  • if the dictionary has 3500 on any key it should be true – Juan Jul 24 '13 at 22:30
  • That isn't a valid answer. What should the array contain. Show an example input and expected output. You have multiple dictionaries, you need to be more clear and specific. – Wain Jul 24 '13 at 22:34
  • As I said in my original post. I'm looking for way to filter the dictionaries where any key contains 3500 – Juan Jul 24 '13 at 22:37
0

you can also use

-keysOfEntriesPassingTest:

of NSDictionary. Just pass in a block like so:

for(NSDictionary *myDictionary in myArray){ 
    NSSet *resultSet = [myDictionary keysOfEntriesPassingTest:^(id key, id object, BOOL *stop) {
        //assuming that 3500 is an int, otherwise use appropriate condition.
        if([[NSNumber numberWithInt:object] isEqual:[NSNumber numberWithInt:3500]]){
            return YES;
        }else{
            return NO;
        }
     }];
    if (resultSet.count>0){
        //do whatever to myDictionary
    }
}
katzenhut
  • 1,742
  • 18
  • 26