0

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)

2
  • 1
    tried parsing it? var nativetestTemplates = JSON.parse(json_encoded_string); Commented Oct 8, 2014 at 12:33
  • At which step, and how will it benefit? I have now corrected the mistake, as pointed out by Djave. Commented Oct 8, 2014 at 13:38

2 Answers 2

3

Don't wrap it in quotes, you are saying

var nativetestTemplates = "[145]";

you want it to say

var nativetestTemplates = [145];

So, your code would read:

var nativetestTemplates = <?php echo json_encode($NATIVE_TEST_TEMPLATE_IDS); ?>; //PHP array into JSON.

The reason that you are getting the weird output ([, 1, 2 etc) is that Javascript is just finding the character 0, 1, 2 characters from the start of the string, rather than the item at the start of the array.

Edit

Funnily enough its the same issue, you are muddling up your variable types again. Because you are getting the val() of (I imagine) an input field, you are looking for a string again.

console.log(typeof($("#adnetwork_type").val())); // Logs 'string'

Don't fret though, you can fix this.

var testAdnetworkId = parseInt($("#adnetwork_type").val()); //reading selected dropdown value

parseInt() is your friend. It tries its best to replace a string or other variable with a number, which is what you want. You will see here http://codepen.io/EightArmsHQ/pen/Kjgfh that it works.

More on variable types: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out the mistake. But I am still having an issue. Please check my question update.
0

you need to parse json variable in javascript as following:

nativetestTemplates = JSON.parse(nativetestTemplates);

Now nativetestTemplates.length will give you correct result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.