Posted By

jlvallelonga on 02/17/09


Tagged

javascript array Value find in


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

brent-man
sheedy
guido


is value in array


 / Published in: JavaScript
 

this function tells you whether or not the passed value is in the passed array

  1. function isValueInArray(arr, val) {
  2. inArray = false;
  3. for (i = 0; i < arr.length; i++)
  4. if (val == arr[i])
  5. inArray = true;
  6. return inArray;
  7. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: sheedy on September 19, 2009

This is exactly what I needed! Thanks!

Posted By: AlexG on March 20, 2011

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; }

Posted By: tlacaelelrl on June 16, 2012

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!

You need to login to post a comment.