Check if an array item is set in JS
问题 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 回答1: Use the in keyword to test if a attribute is defined in a object if (assoc_var in assoc_pagine) OR if ("home" in assoc_pagine) There are quite a few issues here. Firstly, is var supposed to a variable has the value "home", "work" or "about"