/ Published in: JavaScript
this function tells you whether or not the passed value is in the passed array
Expand |
Embed | Plain Text
function isValueInArray(arr, val) { inArray = false; for (i = 0; i < arr.length; i++) if (val == arr[i]) inArray = true; return inArray; }
Comments
Subscribe to comments
You need to login to post a comment.

This is exactly what I needed! Thanks!
Function is good, but it can be even better. There is no need to continue loop if element was found. And no boolean variable is needed in the following function:
function isValueInArray(arr, val) { for (i = 0; i < arr.length; i++) if (val == arr[i]) return true; return false; }
jlvallelonga, I really have no idea what is been going on with me, I have been trying to get to exclude some values from an array an it either crashes the browser or it does not render the content, I tried $.inArray() from jQuery and convert to object with in from plain javascript and nothing seemed to work, I did not want to make my own function and after I saw yours creating my own function was what helped I think that libraries do help sometimes but they are not made for every situation, your function works excellent, thank you!