-1
{"result":[[{"Image":{"__type":"File","name":"tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg","url":"http://f
iles.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae9/tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg"},"Tit
le":"The Expendables 3","featured_actors":" Sylvester Stallone, Jason Statham, Jet Li","genre":"Action, Adventure, Thril
ler","imdb_rating":6.3,"plot_summary":"Barney augments his team with new blood for a personal battle: to take down Conra
d Stonebanks, the Expendables co-founder and notorious arms trader who is hell bent on wiping out Barney and every singl
e one of his associates.","running_time_mins":126,"trailer_video_link":"http://www.imdb.com/rg/VIDEO_PLAY/LINK//video/im
db/vi1896197145/","viewer_rating":"18","objectId":"8cTkg769gM","createdAt":"2014-08-16T21:32:28.318Z","updatedAt":"2014-
08-16T21:35:56.217Z","__type":"Object","className":"Movie"}],[{"Image":{"__type":"File","name":"tfss-65707002-e78b-47bd-
9c7a-da072f388e25-deliver.jpg","url":"http://files.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae9/tfss-65707002-e78b
-47bd-9c7a-da072f388e25-deliver.jpg"},"Title":"Deliver Us From Evil","featured_actors":"Eric Bana, Édgar Ramírez, Oliv
ia Munn","genre":"Crime, Horror, Thriller","imdb_rating":6.5,"plot_summary":"New York police officer Ralph Sarchie inves
tigates a series of crimes. He joins forces with an unconventional priest, schooled in the rites of exorcism, to combat
the possessions that are terrorizing their city.","running_time_mins":118,"trailer_video_link":"http://www.imdb.com/vide
o/imdb/vi1746054425/?ref_=tt_ov_vi","viewer_rating":"15","objectId":"BoCM1zbOvh","createdAt":"2014-08-25T14:27:24.331Z",
"updatedAt":"2014-08-25T19:49:09.910Z","__type":"Object","className":"Movie"},{"Image":{"__type":"File","name":"tfss-772
aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg","url":"http://files.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae
9/tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg"},"Title":"The Expendables 3","featured_actors":" Sylvester
Stallone, Jason Statham, Jet Li","genre":"Action, Adventure, Thriller","imdb_rating":6.3,"plot_summary":"Barney augments
 his team with new blood for a personal battle: to take down Conrad Stonebanks, the Expendables co-founder and notorious
 arms trader who is hell bent on wiping out Barney and every single one of his associates.","running_time_mins":126,"tra
iler_video_link":"http://www.imdb.com/rg/VIDEO_PLAY/LINK//video/imdb/vi1896197145/","viewer_rating":"18","objectId":"8cT
kg769gM","createdAt":"2014-08-16T21:32:28.318Z","updatedAt":"2014-08-16T21:35:56.217Z","__type":"Object","className":"Mo
vie"}]]}

This is my JSON response using curl. I am trying to retrieve unique objects from the array. As you can see, it returned a duplicate of the first object in the end. I have tried a lot of solutions even the Underscore.js _.uniq method. I have still been unable to achieve the proper result. I would appreciate any help on this. Thanks in advance.

This is my implementation

Parse.Promise.when(promises).then(
    function() {
        var result = [];
        _.each(arguments, function(object) {
            result.push(object); // each object is an array of movieIds
        });

        var movies = _.flatten(result);
        var uniqueMovies = _.uniq(movies, function(movie){
                                            return movie.Title;
        });

        response.success(uniqueMovies);
    },
    function(error) {
        response.error(error);
    }
);
///
});
cweiske
  • 30,033
  • 14
  • 133
  • 194
user3679294
  • 13
  • 1
  • 7

1 Answers1

0

I noticed that the duplicate "image" object you mention attaches inside the second one so i found a solution by iterating through this json object and take only the first element of each iteration. Take a look at this one: http://jsfiddle.net/csdtesting/527oevbc/

    var data = {
      "result": [
        [{
          "Image": {
            "__type": "File",
            "name": "tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg",
            "url": "http://f iles.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae9/tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg"
          },
          "Title": "The Expendables 31",
          "featured_actors": " Sylvester Stallone, Jason Statham, Jet Li",
          "genre": "Action, Adventure, Thril ler",
          "imdb_rating": 6.3,
          "plot_summary": "Barney augments his team with new blood for a personal battle: to take down Conra d Stonebanks, the Expendables co-founder and notorious arms trader who is hell bent on wiping out Barney and every singl e one of his associates.",
          "running_time_mins": 126,
          "trailer_video_link": "http://www.imdb.com/rg/VIDEO_PLAY/LINK//video/im db/vi1896197145/",
          "viewer_rating": "18",
          "objectId": "8cTkg769gM",
          "createdAt": "2014-08-16T21:32:28.318Z",
          "updatedAt": "2014- 08-16T21:35:56.217Z",
          "__type": "Object",
          "className": "Movie"
        }],
        [{
          "Image": {
            "__type": "File",
            "name": "tfss-65707002-e78b-47bd- 9c7a-da072f388e25-deliver.jpg",
            "url": "http://files.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae9/tfss-65707002-e78b -47bd-9c7a-da072f388e25-deliver.jpg"
          },
          "Title": "Deliver Us From Evil",
          "featured_actors": "Eric Bana, Édgar Ramírez, Oliv ia Munn",
          "genre": "Crime, Horror, Thriller",
          "imdb_rating": 6.5,
          "plot_summary": "New York police officer Ralph Sarchie inves tigates a series of crimes. He joins forces with an unconventional priest, schooled in the rites of exorcism, to combat the possessions that are terrorizing their city.",
          "running_time_mins": 118,
          "trailer_video_link": "http://www.imdb.com/vide o/imdb/vi1746054425/?ref_=tt_ov_vi",
          "viewer_rating": "15",
          "objectId": "BoCM1zbOvh",
          "createdAt": "2014-08-25T14:27:24.331Z",
          "updatedAt": "2014-08-25T19:49:09.910Z",
          "__type": "Object",
          "className": "Movie"
        }, {
          "Image": {
            "__type": "File",
            "name": "tfss-772 aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg",
            "url": "http://files.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae 9/tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg"
          },
          "Title": "The Expendables 3",
          "featured_actors": " Sylvester Stallone, Jason Statham, Jet Li",
          "genre": "Action, Adventure, Thriller",
          "imdb_rating": 6.3,
          "plot_summary": "Barney augments  his team with new blood for a personal battle: to take down Conrad Stonebanks, the Expendables co-founder and notorious  arms trader who is hell bent on wiping out Barney and every single one of his associates.",
          "running_time_mins": 126,
          "tra iler_video_link": "http://www.imdb.com/rg/VIDEO_PLAY/LINK//video/imdb/vi1896197145/",
          "viewer_rating": "18",
          "objectId": "8cT kg769gM",
          "createdAt": "2014-08-16T21:32:28.318Z",
          "updatedAt": "2014-08-16T21:35:56.217Z",
          "__type": "Object",
          "className": "Mo vie"
        }]
      ]
    }
    var jsonArray = data.result;
    $.each(jsonArray, function(index, data) {
      //adds all this values fron the array ,only from and to
      //console.log(data[0]);
      //alert(data[0].Title);
      //console.log(data[0].Title);
      document.write("-->" + data[0].Title);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Hope that is exactly what you want!
Giannis Grivas
  • 3,374
  • 1
  • 18
  • 38
  • I don't understand your solution. Also, you edited the titles to be different. Why is that? – user3679294 Oct 04 '14 at 21:42
  • i used http://jsonviewer.stack.hu/ to format .i didnt change anything.Uncomment the console.log to see the objects you will understand – Giannis Grivas Oct 04 '14 at 21:44
  • Is there an Underscore.js implementation of this? Because, that's what I'm restricted and $(#options) is not recognized. – user3679294 Oct 04 '14 at 21:50
  • Options removed .is not for here. The solution is custom.You asked sommething and is fixed. the result is what you want i think – Giannis Grivas Oct 04 '14 at 21:57
  • I Have tested the same code with underscore.js which I am using and it doesn't give the same result. Please check with Underscore if you can. thanks – user3679294 Oct 04 '14 at 22:17