I have an array of values as shown below
var inputValues = ["java","php","python"] //values from user
var responseValues = [{"name":"mysql"}, {"name":"golang"}, {"name":"svelte"}] //response from JSON
I need to compare the input array with the existing data in the response JSON before inserting the input array.
If all the values in the input array match any value in the response array, an alert will be shown using JavaScript. If none of the values in the input array matches with any value in the response array, all the values from the input array will be inserted.
I have successfully created a code to insert the array using $.each
.
I would like to know how to filter and check the input values against the response JSON data before performing the insertion.
Below is the code I am currently using to insert the array
var counter = 0;
var totalInputs = inputValues.length;
$.each(inputValues, function (index, value) {
$.ajax({
url:"<?= base_url();?>controller/function",
type:"POST",
dataType:"json",
data:{"name":value},
success: function(response){
counter++;
if (counter == totalInputs)
{
setTimeout(() => {
Swal.fire({
icon: 'success',
title: 'Success',
timer: 1500,
showConfirmButton: false
}).then((result) => {
location.reload();
});
}, 500);
}
}
})
})