Got bored … so I quickly coded this function to check whether values are present in an array. You can also provide multiple values to search for (using an array of values) and a mode for the prototype to check by. Mode “&&” requires all the values in the array to be present, mode “||” requires at least one of the values to be present.
Array.prototype.valueExists = function(val, mode) {
if (val instanceof Array) {
if (mode != "&&" && mode != "||") mode = "&&";
if (mode == "&&") {
for (var all in val) {
if (!this.valueExists(val[all])) {
return false;
break;
}
}
return true;
} else if (mode == "||") {
for (var all in val) {
if (this.valueExists(val[all])) {
return true;
break;
}
}
return false;
}
} else {
for (var all in this) {
if (this[all] == val) {
return true;
break;
}
}
return false;
}
};
test = new Array("Array", ".", "prototype", ".", "valueExists");
testByValue = "Array";
testByMultiple = [".", "proto"];
trace(test.valueExists(testByValue)); // true
trace(test.valueExists(testByMultiple, "&&")); // false
trace(test.valueExists(testByMultiple, "||")); // true