views:

337

answers:

1

So the documentation has this nifty "in" operator which I like the idea of more that using a multiple step or statement (||). The documentation gives me this example.

trace("PI" in Math);         // true
trace("myProperty" in Math); // false

and

public var myArray:Array = ["zero", "one", "two"];
trace(0 in myArray); // true
trace(1 in myArray); // true
trace("two" in myArray); // true
trace(3 in myArray); // false

So I try to use it like this:

var quickArray:Array = ["@icd9_color","@icd9_icd9","@templateIcd9_name","@templateIcd9_name","@templateIcd9_templateIcd9ID"];
return (element.dataField in quickArray);

Now I can trace or Alert.show() the element.datafield and it will match exactly with an array item, but it never returns true. Can anyone help me figure out why?

The only thing I can get to work this ugly thing:

return (
  element.dataField == "@icd9_color" ||
  element.dataField == "@icd9_icd9"
  etc..
)
+3  A: 

The in operator checks whether an object has a specified property - not what the value of that property is.

You want to use Array.indexOf and check for a non-negative value.

Anon.
Thanks I was getting quite aggravated by that.
invertedSpear
So shouldn't trace("two" in myArray); output false instead of true in invertedSpear's test?
bug-a-lot