JQuery - Compare 2 arrays in one each

0

I have the following function:

function pre() {
        $.ajax({
            url: "arquivo1.php",
            method: 'POST',
            dataType: 'json',
            success: function (data) {
                $.ajax({
                    url: "arquivo2.php",
                    method: 'POST',
                    dataType: 'json',
                    success: function (data2) {

                    $.each(data.id, function(i, item) {

                        $( ".lado" ).prepend("<div id='" + data.id + "'><select class='select'></select></div>");

                        if (data.id == data2.id) {
                            $( ".select" ).append("<option'>" + data2.id + "</option>");
                        }
                    });

                }
            });
        }
    });

}

There are 2 ajax within each other that need to compare 2 different tables and generate selects list and selects options, if separates options that are not from such select.

    
asked by anonymous 28.07.2016 / 22:25

2 answers

1

In this case you need 2 $.each . So:

    // primeiro laço 
    $.each(data, function(key_data_first, item_first) {

        // segundo laço
        $.each(data, function(key_data_second, item_second) {

            // cria o select
            $( ".lado" ).prepend("<div id='" + item_first.id + "'><select id='select_" + item_first.id + "' class='select'></select></div>");

            //insere as opções no select
            if (item_first.id == item_second.id) {
                $("#select_"+item_first.id).append("<option>" + item_second.id + "</option>");
            }
        });
    });
    
28.07.2016 / 23:15
0

If what is inside the id of each data is an array, reason to use $.each , you can do this:

$.each(data.id, function(i, item) {
  $(".lado").prepend("<div id='" + data.id + "'><select class='select'></select></div>");
  if (item == data2.id[i]) {
    $(".select").append("<option'>" + data2.id + "</option>");
  }
});
    
28.07.2016 / 23:16