I have this code
var customer_number = $("#customer_number").val();
var data = <?php if(isset($questions)) {echo $questions; } else {echo '"";';} ?>
var records = [];
$.each(data, function(key,value) {
$.ajax({
url: "api/suppression/checkifsuppressed",
type: 'GET',
data: {'customer_number':customer_number, 'columnheader' : value.columnheader},
success: function(result){
if(parseInt(result) >= 1)
{
data.splice(key , 1);
}
}});
});
$.each(data, function(key,value) {
records.push({
agebracket: value.agebracket,
agerestriction: value.agerestriction,
child_count: value.child_count,
child_enable_response: value.child_enable_response,
child_sort_num: value.child_sort_num,
columnheader: value.columnheader,
costperlead: value.costperlead,
created_at: value.created_at,
deliveryassignment: value.deliveryassignment,
id: value.id,
is_child: value.is_child,
isenabled: value.isenabled,
ownhomeoptions: value.ownhomeoptions,
ownhomerestriction: value.ownhomerestriction,
parent_colheader: value.parent_colheader,
po_num: value.po_num,
postcodeexclusion: value.postcodeexclusion,
postcodeinclusion: value.postcodeinclusion,
postcoderestriction: value.postcoderestriction,
question: value.question,
sortorder: value.sortorder,
telephoneoptions: value.telephoneoptions,
telephonerestriction: value.telephonerestriction,
updated_at: value.updated_at,
parent_enable_response: value.parent_enable_response,
child_lead_respponse: value.child_lead_respponse,
});
});
console.log("The new data is..");
console.log(data);
console.log("The new records is..");
console.log(records);
What I am doing is I have the variable data
which has content in it let's say two records. And as you can see I have two $.each
loop my question is that, are those two loop being executed at the same time? What I have in my first $.each
is I loop in all the records of the data
then for each record I am making an ajax call to check something in the database, if the database returns 1 or greater than 1 I will remove that current data in the data
array. So I made a test.
The data
has two records and will satisfy the condition in the first $.each
which will remove one data in the array.
Then in my second $.each
I'm just getting the data
variable loop in it and whatever the content of it I am just copying it to my records
array. But when I run the code I got this
As you can see, the data
has now 1 record which I expected because it satisfies one condition in the fist loop. However in the result of my records
array, why it has still 2 records in it? Did it get the original value of the data
which has two records in it? It should be 1 because on the first loop I already modified the data
array? This code will run on page load.
I'm new to jQuery.