Search code examples
iosobjective-cnsmutablearraynsmutabledictionary

Split array of dictionaries into sub-arrays


Below is a part of my JSON response where 'results' is an array of dictionaries :

{
  "results": [
    {
      "id": 6,
      "genre_name": "Action",
      "cover_image": "http://54.254.204.81/images/Action.png"
    },
    {
      "id": 5,
      "genre_name": "Adventure",
      "cover_image": "http://54.254.204.81/images/Adventure.png"
    },
    {
      "id": 4,
      "genre_name": "Romance",
      "cover_image": "http://54.254.204.81/images/Romance.png"
    },
    {
      "id": 3,
      "genre_name": "Sci-Fci",
      "cover_image": "http://54.254.204.81/images/Sci-Fi.png"
    },
    {
      "id": 1,
      "genre_name": "Guide",
      "cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
    },
    {
      "id": 2,
      "genre_name": "Horror",
      "cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
    },
    {
      "id": 7,
      "genre_name": "Emotional",
      "cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
    },
    {
      "id": 8,
      "genre_name": "abcd",
      "cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
    }
  ]
}

Now I have another JSON response where 'genres' is an array which contains objects which are subset of 'results' array objects with key 'id'.

{
  "genres": [
    3,
    1
  ]
}

Now, is it possible for me to split 'results' into two arrays 'results1' and 'results2' like:

{
  "results1": [
    {
      "id": 6,
      "genre_name": "Action",
      "cover_image": "http://54.254.204.81/images/Action.png"
    },
    {
      "id": 5,
      "genre_name": "Adventure",
      "cover_image": "http://54.254.204.81/images/Adventure.png"
    },
    {
      "id": 4,
      "genre_name": "Romance",
      "cover_image": "http://54.254.204.81/images/Romance.png"
    },
    {
      "id": 2,
      "genre_name": "Horror",
      "cover_image": "http://54.254.204.81/images/Phineas-and-Ferb-Christmas-Wallpaper.jpg"
    },
    {
      "id": 7,
      "genre_name": "Emotional",
      "cover_image": "http://54.254.204.81/images/a0fea991287cf41b6b9c4aa16196517f.jpg"
    },
    {
      "id": 8,
      "genre_name": "abcd",
      "cover_image": "http://54.254.204.81/images/logo_text_S0KyzUW.png"
    }
  ]
}

and

{
  "results2": [
    {
      "id": 3,
      "genre_name": "Sci-Fci",
      "cover_image": "http://54.254.204.81/images/Sci-Fi.png"
    },
    {
      "id": 1,
      "genre_name": "Guide",
      "cover_image": "http://54.254.204.81/images/Adventure_XHLbNfN.png"
    }
  ]
}

Solution

  • For that you need to use NSPredicate with IN and NOT like this way.

    First create resultArray and genresArray from your two JSON responses.

    NSArray *resultArray = [firstJSONResponse objectForKey:@"results"];
    NSArray *genresArray = [secondJSONResponse objectForKey:@"genres"];
    

    Now filter your resultArray using NSPredicate to get your result.

    For Result1

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (id IN %@)",genresArray];
    NSArray *firstSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
    NSLog(@"%@",firstSplitArray);
    

    For Result2

    predicate = [NSPredicate predicateWithFormat:@"id IN %@",genresArray];
    NSArray *secondSplitArray = [resultArray filteredArrayUsingPredicate:predicate];
    NSLog(@"%@",secondSplitArray);