0

Would anyone know how to filter out repeated data from an array of objects? I have a simple parent and child list. The child list has a field with the parent's ID.

Here is an exmaple of what my child data looks like:

responseText: {
    d: {
        results: [{
            ID_of_parent: "17",
            Day_or_night: "day",
            Start: "2016-06-01 08:00",
            End: "2016-06-01 10:00",
            Hours: "2"
        }, {
            ID_of_parent: "17",
            Day_or_night: "day",
            Start: "2016-06-01 13:00",
            End: "2016-06-01 14:00",
            Hours: "1"
        }, {
            ID_of_parent: "17",
            Day_or_night: "night",
            Start: "2016-06-01 21:00",
            End: "2016-06-01 22:00",
            Hours: "1"
        }, {
            ID_of_parent: "22",
            Day_or_night: "day",
            Start: "2016-06-01 09:00",
            End: "2016-06-01 10:00",
            Hours: "1"
        }, {
            ID_of_parent: "22",
            Day_or_night: "day",
            Start: "2016-06-01 14:00",
            End: "2016-06-01 15:00",
            Hours: "1"
        }, {
            ID_of_parent: "54",
            Day_or_night: "day",
            Start: "2016-06-01 13:30",
            End: "2016-06-01 16:00",
            Hours: "2.5"
        }]
    }
}

And here is the parent data:

responseText: {
    d: {
        results: [{
            ID: "17",
            description: "Description 1"
        }, {
            ID: "22",
            description: "Description 2"
        }, {
            ID: "34",
            description: "Description 3"
        }, {
            ID: "54",
            description: "Description 4"
        }]
    }
}

What im simply trying to accomplish is to get the child lists data via ajax then foreach, remove the duplicate IDs and then get me the parent ID. At the moment this is what I have so far:

  $.ajax({
    url:"Child_list_path",           
    dataType: "json",
    cache: false,
    success: function (child) {

     $.each(child.d.results, function (index, children) {

            $.ajax({
                url: parent_list_path("+children.Incident_ID+")",//filter by ID           
                dataType : 'json',
                cache : false,
                success : function (parent) {   
                //stuff
                }
            });//end of inner ajax

     });//end of each

    }//sucess end
  });

Problem with this is that that in the child data, there can be multiple entries that have the same parent's ID. So what happens during the for each sequence is that it will pull the filtered parent data multiple times for the same ID when I just need to get the data for each unique IDs.

EDIT: My objective is to be able to filter repeated ID_of_parent in the child data. So instead of looping trough 17,17,17,22,22,54 I just want it to loop trough 17,22,54.

Daniel Ellison
  • 1,339
  • 4
  • 27
  • 49
  • What are expected results? Also is API returning same child data for every request? Seems strange that you provide specific ID to `parent_list_path()` but get response with other ID's – charlietfl Jun 04 '16 at 16:52
  • I just need it to return unique IDs each time only to allow me to get the proper parent data associated child's ID_of_parent. So im expecting to return simply 17, 22, 54. – Daniel Ellison Jun 04 '16 at 16:53
  • So you need to map response to extract ID only? As array? Objective still not totally clear – charlietfl Jun 04 '16 at 16:56
  • Yes exactly, sorry I wasnt clear. I really only need each unique ID_of_parent of the child data. instead of geting 17,17,17,22,22,54 while looping trough the child data. I only need it to somehow loop only the unique IDs such as 17,22,54. – Daniel Ellison Jun 04 '16 at 16:59
  • You just need a way to get unique array? So, check out [this simple and comprehensive answer](http://stackoverflow.com/a/9229821/4751310) – Jinyoung Kim Jun 04 '16 at 17:01
  • So map ID's to new array ... check if ID already exists before pushing it again. Very simple loop and use `indexOf()` to check if it already exists – charlietfl Jun 04 '16 at 17:01
  • Ok thanks I was hoping there was an easy way directly trough the ajax request to do that. But ill look at those options, thanks guys. – Daniel Ellison Jun 04 '16 at 17:03
  • Would be better if you showed what you are doing now so we can help fix that code – charlietfl Jun 04 '16 at 17:03
  • Can't you do it server side? Much more efficient, probably – StudioTime Jun 04 '16 at 17:04
  • No I dont have access to the server side :(. – Daniel Ellison Jun 04 '16 at 17:05

1 Answers1

0

Figured it out thanks to your comments. This is what I ended up doing to fix my problem.

 $.ajax({
    url:"Child_list_path",           
    dataType: "json",
    cache: false,
    success: function (child) {

        child_temp_IDs = [];

        $.each(child.d.results, function (index, children) {
            if($.inArray(children.Incident_ID, child_temp_IDs) === -1) child_temp_IDs.push(children.Incident_ID);
        });//end of each


     $.each(child_temp_IDs , function (index, children) {

            $.ajax({
                url: parent_list_path("+children+")",       
                dataType : 'json',
                cache : false,
                success : function (parent) {   

                }
            });

     });

    }
  });
Daniel Ellison
  • 1,339
  • 4
  • 27
  • 49