2

Here i have two array's like first array is,

[
  "A",
  "B",
  "A",
  "B",
  "N",
  "B"
]

second array like,

[
  {
    "A": "2,141.8"
  },
  {
    "B": "2,141.8"
  },
  {
    "A": "2,141.8"
  },
  {
    "B": "2,376"
  },
  {
    "N": "2,376"
  },
  {
    "B": "2,376"
  },

]

But i need to compare first array objects into the second array,if it is equal to both Key and object values i want like this,

{
  "A": [
    {
      "A": "2,141.8"
    },
    {
      "A": "2,141.8"
    }
  ],
  "B": [
    {
      "B": "2,376"
    },
    {
      "B": "2,376"
    }
  ]
}

Can you please suggest me how can i implement this,Thank you.

2
  • 5
    have you added wrote any code for doing this. Commented Jan 10, 2017 at 6:47
  • What you have tried yet? Commented Jan 10, 2017 at 6:56

2 Answers 2

1

this will work.

 NSArray *arr1 = [NSArray arrayWithObjects:@"A",@"B",@"A",@"B",@"N",@"B", nil];
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:arr1];
NSArray *arr2 = [orderedSet array];
NSArray *arr3 = [NSArray arrayWithObjects:
                 @{@"A": @"2,141.8"},
                 @{@"B": @"2,141.8"},
                 @{@"A": @"2,141.8"},
                 @{@"B": @"2,376"},
                 @{@"N": @"2,376"},
                 @{@"B": @"2,376"}, nil];
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i = 0 ; i <= arr2.count - 1; i++) {
    NSMutableArray *arr4 = [[NSMutableArray alloc] init];
    for (int j = 0 ; j <= arr3.count - 1; j++) {
        if ([[arr3 objectAtIndex:j] objectForKey:[arr2 objectAtIndex:i]]) {
            [arr4 addObject:[arr3 objectAtIndex:j]];
        }
    }
    if (arr4.count != 0) {
        [arr addObject:@{[arr2 objectAtIndex:i]:arr4}];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Vaibby, but here i need to only equal key and object values like this, [ { "A": [ { "A": "2,141.8" }, { "A": "2,141.8" } ] }, { "B": [ { "B": "2,376" }, { "B": "2,376" } ] }, ]
Yes i did whatever my requirement.Thanks for ur answer.@vaibby
0

I did try using NSPredicate on this.

Given:

NSArray *keys = @[@"A",@"B",@"A",@"B",@"N",@"B"];
NSArray *sample = @[@{@"A":@"1234"},@{@"B":@"1234"},@{@"N":@"1234"},@{@"B":@"1234"},@{@"A":@"1234"},@{@"B":@"1234"}];

Get the distinct keys

NSArray *distinctKeys = [keys valueForKeyPath:@"@distinctUnionOfObjects.self"];

__block NSMutableDictionary *result = [NSMutableDictionary new]; // declare container

Filter the array using predicate by enumerating the distinct keys

[distinctKeys enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    // Assume that there wont be a nil value
    NSPredicate *predicator = [NSPredicate predicateWithFormat:@"%K != nil",obj];
    NSArray *filtered = [sample filteredArrayUsingPredicate:predicator];
    result[obj] = filtered;
}];

Result:

{
A =     (
            {
        A = 1234;
    },
            {
        A = 1234;
    }
);
B =     (
            {
        B = 1234;
    },
            {
        B = 1234;
    },
            {
        B = 1234;
    }
);
N =     (
            {
        N = 1234;
    }
);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.