Basically, I'm producing a JSON Array dynamically to Javascript, I'm then doing an Ajax call which returns JSON data from an API, I want to check an value inside the objects from the Ajax JSON response to see if it matches with my JSON Array, I've tried doing the following :
My Dynamic JSON Array - JSON.php
global $wpdb;
$Ids = array();
$table_name = $wpdb->prefix . "videos";
$idList = $wpdb->get_results("SELECT `videoId` FROM `$table_name`");
foreach($idList as $ID) {
$Ids[] = $ID->videoId;
}
echo json_encode(array('IdList' => $Ids));
My Dynamic JSON output example - JSON.php
{"IdList":["47","55"]}
My Dynamic JSON Array - Javascript
var Ids = null;
jQuery.ajax({
type: "GET",
url: pluginURL + "/JSON.php",
data: "ajaxAction=getIds&rand="+Math.floor(Math.random()*10000),
success: function(data) {
Ids = jQuery.parseJSON(data);
},
error: function() {
alert("Error");
}
});
So that's the code that's producing an Array of ID's that are stored inside my Database, I then want to use an AJAX call which returns an JSON Response from an API to check against these IDs, Here's the code that's returning the API response
jQuery.ajax({
type: "GET",
url: apiURL + "video/" + Username,
success: function(data) {
Parsed = jQuery.parseJSON(data);
jQuery.each(Parsed.user_media, function(i,v){
id = v['id'];
if(jQuery.inArray(id,Ids['IdList']) !== -1) {
//this isn't working!
}
});
},
});
Example data output from the AJAX call
{"user_media":[{"id":"2"},{"id":"44"}]}
As you can see I've tried to utilise the jQuery.inArray()
to no avail, I'm not sure why, as I may just be parsing something wrong