IsIn For Arrays | ||
| ||
| for some things. I once tried to use am isIn function on an array and found it to be absent. Or at at least it was at the time. This was some time ago back in the early days of JS. So I wrote this little snippet and I think many beginners might find it useful and maybe use it even if they didn’t know they needed it. The function takes two arguments an array and a value. The value is first and the array second. Why? Simple “Hey Marilyn is the burrito still in the fridge?” What is this man talking about? Simple the idea that this function is essentially a question. It’s asking if something is in something else. I’ve yet to pose the question “Hey, Marilyn does the fridge have a burrito in it?” It’s rather awkward and no one likes that so to remember parameter orders I like to put them in the order that they would be if I asked it as a question. The internal names are basically what they are. theArray is “the array” and theValue is “the value” you’re looking for. checkValue is the iteration of the loop and thus what value in “the array” or theArray you’re looking for. function isIn(theValue, theArray) { for(checkValue = 0; checkValue < theArray.length-1; checkValue++){ if((theArray[checkValue] == theValue)) return true; } return false; } | ||
| | ||
| |