views:

225

answers:

4

Hi, 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

A: 

Its not working because var is unquoted in assoc_page[var].

Also, You're not using typeof to get the defined|undefined state of assoc_var, your directly quering the value of assoc_var.

To fix, put var inside quotes and use typeof instead:

if( typeof assoc_pagine["var"] != "undefined" ) 

update: As other answers have indicate, using a array is not the best sollution for this problem. Consider using a Object instead.

var assoc_pagine = new Object();
assoc_pagine["home"]=0;
assoc_pagine["about"]=1;
assoc_pagine["work"]=2;

And test if a attribute is defined by using in

if( var in assoc_pagine )

note: var is a reserved word in JavaScript, don't use it for a variable name.

NixNinja
I really don't get the downvotes... OP had problem, instead of being very clever and posting a 'Better Sullotion' i just helped him understand his problem.
NixNinja
A: 

var is a statement... so it's a reserved word... So just call it another way. And that's a better way of doing it (=== is better than ==)

if(typeof array[name] !== 'undefined') {
    alert("Has var");
} else {
    alert("Doesn't have var");
}
xavierm02
Could someone explain me why I'm -2 ?
xavierm02
same answer, same problem. lol.. i guess they don't like simple answers...
NixNinja
+4  A: 
var assoc_pagine = new Array();
assoc_pagine["home"]=0;

Don't use an Array for this. Arrays are for numerically-indexed lists. Just use a plain Object ({}).

What you are thinking of with the 'undefined' string is probably this:

if (typeof assoc_pagine[key]!=='undefined')

This is (more or less) the same as saying

if (assoc_pagine[key]!==undefined)

However, either way this is a bit ugly. You're dereferencing a key that may not exist (which would be an error in any more sensible language), and relying on JavaScript's weird hack of giving you the special undefined value for non-existent properties.

This also doesn't quite tell you if the property really wasn't there, or if it was there but explicitly set to the undefined value.

This is a more explicit, readable and IMO all-round better approach:

if (key in assoc_pagine)
bobince
The only answer that seems to address the obvious problem of using an array as an associative array. +1
J-P
(Having said that, it *does* work, because `Array` is a subclass of `Object`. It just doesn't get you anything, other than a load more properties on the prototype to potentially clash with.)
bobince
+1  A: 

This is not an Array. Better declare it like this:

var assoc_pagine = {};
assoc_pagine["home"]=0;
assoc_pagine["about"]=1;
assoc_pagine["work"]=2;

or

var assoc_pagine = {
                 home:0,
                 about:1,
                 work:2
               };

To check if an object contains some label you simply do something like this:

if('work' in assoc_pagine){
   // do your thing
};
KooiInc
Oops, overlooked Bobinces answer, which tells you all and more than you wanted to know
KooiInc