0

I want to Merge Two Array With Same Index, This is my JSON

"tier_info": [
    {
        "tier_id": "1",
        "tier_name": "tier-1",
        "price": "3.9",
        "ios_id": "tier-1",
        "android_id": "tier-1"
    },
    {
        "tier_id": "2",
        "tier_name": "tier-2",
        "price": "4.9",
        "ios_id": "tier-2",
        "android_id": "tier-2"
    },
    {
        "tier_id": "3",
        "tier_name": "tier-3",
        "price": "5.9",
        "ios_id": "tier-3",
        "android_id": "tier-3"
    },
    {
        "tier_id": "4",
        "tier_name": "free",
        "price": "0",
        "ios_id": "free",
        "android_id": "free"
    }
]

I'm using custom picker, Now I want "tier_name" with "price" in same array index [e.g. "tier_1 : 3.9"]. Code of merging two Array:

    NSMutableArray *tierTitles = [[NSMutableArray alloc] init];
    tierTitles = [tierArray valueForKey:@"tier_name"];
    NSMutableArray *tierPrice = [[NSMutableArray alloc] init];
    tierPrice = [tierArray valueForKey:@"price"];

    NSMutableArray *combined = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < tierArray.count; i++) {
        [combined addObject: @{tierTitles[i]:tierPrice[i]}];
    }

and i got this

(
    {
    "tier-1" = "3.9";
},
    {
    "tier-2" = "4.9";
},
    {
    "tier-3" = "5.9";
},
    {
    free = 0;
}
)

i want it like :

  (
    "tier-1" = "3.9";
    "tier-2" = "4.9";
    "tier-3" = "5.9";
    free = 0;
  )

what am i doing wrong here..any correct way to do this?

6
  • What is your answer expected type ? It looks like an Array of Dictionary, so the previous result is ok to me Commented Jul 6, 2018 at 7:34
  • i'm getting combined array in NSMutable Array Commented Jul 6, 2018 at 7:35
  • 1
    You want {"tier-1" = "3.9"; "tier-2" = "4.9"; "tier-3" = "5.9"; free = 0;} (aka one dictionary) not ("tier-1" = "3.9"; "tier-2" = "4.9"; "tier-3" = "5.9"; free = 0;) no? Because yours is something strange, the () in print in Objective-C are for a NSArray, and {} for a NSDictionary, and I don't know what's "tier-1" = "3.9"; in a NSArray. Commented Jul 6, 2018 at 7:41
  • i want in NSMutableArray not in NSDictionary but i got Array of Dictionary... Commented Jul 6, 2018 at 7:53
  • only way to get an array is create a string like "tier-1 = 3.9" that is not appropriate. Commented Jul 6, 2018 at 7:54

2 Answers 2

3

I think what you're asking is you need an NSDictionary that contains both name and price as key-value.You need to enumerate each object and add it to an array if the value exist :-

NSMutableArray *results=[NSMutableArray new];
for (NSDictionary *tier in tierArray]) {
    if ([tier[@"tier_name"] length] && [tier[@"price"] length]) {
        [results addObject:@{tier[@"tier_name"]:tier[@"price"]:}];
    }
}

Or create a Mutable Dictionary Like this:-

NSMutableDictionary *results=[NSMutableDictionary new];
for (NSDictionary *tier in tierArray) {
    if ([tier[@"tier_name"] length] && [tier[@"price"] length]) {
        [results setValue:tier[@"price" forKey:tier[@"tier_name"]];
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

create a Mutable Dictionary is right but same way can i create single Array ?
you can create a array but which data type will you add in your array to add both your tier and price? only possible way to do it do add a dictionary with key value pari @KetanOdedra another alternative would be using a string if you want to add it to an array but i don't think it'll be useful and it'll not make any sense.
no problem at all, let me remove the suggestion : ) @KetanOdedra
0

I think you should add dictionary to your combined array like as below

 [tierTitles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    NSMutableDictionary*combinedDict = [[NSMutableDictionary alloc]init];
    [combinedDict setValue:[tierPrice objectAtIndex:idx] forKey:obj];
    [combined addObject: combinedDict];
}];

1 Comment

its return same which is in my question.

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.