Liquid Fire Posted December 8, 2008 Share Posted December 8, 2008 With PHP, there is a in_array function to see if a value in currently stored in the array. Is there a similar type function for javascript? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 8, 2008 Share Posted December 8, 2008 not that i'm aware of. you can just loop over the elements, or use a JavaScript library like jQuery: http://docs.jquery.com/Utilities/jQuery.inArray Quote Link to comment Share on other sites More sharing options...
Liquid Fire Posted December 8, 2008 Author Share Posted December 8, 2008 This is ironic, I actually use jQuery and was going to ask the jQuery mailing list but i thought that was more of a javascript question than a jQuery question. Thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 8, 2008 Share Posted December 8, 2008 I'd just add a couple of comments: 1) If you do build a function to do this, be sure to "break" out of the loop whne you find the value to prevent unnecessary execution. 2) You can also do this without looping through the array with a little slight of hand. However, it may or may not be more efficient based upon the size of the array. You would also want to use delimiters that would not appear in the data.: (array is the array to be searched) function in_array(searchArray, searchString) { var array_to_string = '~' + searchArray.join('~') = '~'; return ( (array_to_string.search(searchString)!=-1) ? true : false); } Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 8, 2008 Share Posted December 8, 2008 I'd just add a couple of comments: 1) If you do build a function to do this, be sure to "break" out of the loop whne you find the value to prevent unnecessary execution. 2) You can also do this without looping through the array with a little slight of hand. However, it may or may not be more efficient based upon the size of the array. You would also want to use delimiters that would not appear in the data.: (array is the array to be searched) function in_array(searchArray, searchString) { var array_to_string = '~' + searchArray.join('~') = '~'; return ( (array_to_string.search(searchString)!=-1) ? true : false); } you will also want to add '~' to the beginning and end of your search string Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 8, 2008 Share Posted December 8, 2008 you will also want to add '~' to the beginning and end of your search string Yes, I forgot to add that (I also used a '=' instead of '+' when creating the array_to_string to add it at then end of that) - thanks. Fixed: function in_array(searchArray, searchString) { var array_to_string = '~' + searchArray.join('~') + '~'; return ( (array_to_string.search('~'+searchString+'~')!=-1) ? true : false); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.