Server side code
$NATIVE_TEST_TEMPLATE_IDS = array(145);
Client side code to assign PHP array into javascript -
var nativetestTemplates = "<?php echo json_encode($NATIVE_TEST_TEMPLATE_IDS); ?>"; //PHP array into JSON.
But now, the JSON object nativetestTemplates that I have here does not look ready for doing Object instance checks. I need to check if, say, 145 exists in the object nativetestTemplates.
Following are details of console log -
nativetestTemplates.length -> 5
Also, the individual elements of the array have the following -
nativetestTemplates[0] -> [
nativetestTemplates[1] -> 1
nativetestTemplates[2] -> 4
nativetestTemplates[3] -> 5
nativetestTemplates[4] -> ]
So, what did I do wrong and what is the best method to check if an element exists in the object.
Update
After fixing mistake pointed out by Djave, I am still having problem checking if an element exists in the object nativetestTemplates.
Here is my code -
Case 1
var testAdnetworkId = $("#adnetwork_type").val(); //reading selected dropdown value
console.log("see "+$.inArray(testAdnetworkId, nativetestTemplates)+" and "+nativetestTemplates[testAdnetworkId]);
This returns following even when testAdnetworkId has a matching value -
see -1 and undefined
Case 2
But, if I do this -
var testAdnetworkId = 145; //hardcoded integer val
I get the desired output for $.inArray check -
see 0 and undefined
console.dir of testAdnetworkId in case 1 gives this -
0 "1"
1 "4"
2 "5"
However in case 2, it gives -
0 145
Can, someone please explain the proper way to do object property check in Case 1 (reading value of select dropdown)
var nativetestTemplates = JSON.parse(json_encoded_string);