I’ve got an array
var assoc_pagine = new Array();
assoc_pagine["home"]=0;
assoc_pagine["about"]=1;
assoc_pagine["work"]=2;
I tried
if (assoc_pagine[var] != "undefined") {
but it doesn’t seem to work
I’m using jquery, I don’t know if it can help
Thanks
Use the
inkeyword to test if a attribute is defined in a objectOR
There are quite a few issues here.
Firstly, is
varsupposed to a variable has the value “home”, “work” or “about”? Or did you mean to inspect actual property called “var”?If
varis supposed to be a variable that has a string value, please note thatvaris a reserved word in JavaScript and you will need to use another name, such asassoc_var.If you meant to inspect the property called “var”, then you simple need to put it inside of quotes.
Then,
undefinedis not the same as"undefined". You will needtypeofto get the string representation of the objects type.This is a breakdown of all the steps.
So to fix your problem
update: As other answers have indicated, using a array is not the best sollution for this problem. Consider using a Object instead.