I was looking to see if there was an easy way to do membership tests on an array in JS.
Given an array/list:
my_array = ['this', 'that', 1, 3]
I can perform a membership test like so:
>>> 'this' in my_array
True
Javascript, however, has no such function. I came across a nice prototype by a guy named Terence which I have reposted here incase blogger goes the way of delicious/wave/yougettheidea:
Array.prototype.Contains = function(mxd,strict) {
for(i in this) {
if(this[i] == mxd && !strict) return true;
else if(this[i] === mxd) return true;
}
return false;
}
You can use it as such:
> my_array.Contains(2)
false
> my_array.Contains("this")
true
>my_array.Contains(3)
true
