# Array.prototype.valueExists

**URL:** <https://forum.kirupa.com/t/array-prototype-valueexists/153767>\
**Category:** open source\
**Created:** [July 4, 2005, 3:15pm UTC](https://forum.kirupa.com/t/array-prototype-valueexists/153767 "2005-07-04T15:15:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Voetsjoeba](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@Voetsjoeba](https://forum.kirupa.com/u/Voetsjoeba)\
**Post date:** [July 4, 2005, 3:15pm UTC](https://forum.kirupa.com/t/array-prototype-valueexists/153767/1 "2005-07-04T15:15:58Z")

</div>

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.

```auto
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

```
