Possible Duplicate:
Javascript - array.contains(obj)
What's wrong with this:
var zipCodes =(['90001','90002','90003']);
Test if the value exists in the array zipCodes
if('90001' in zipCodes) {
alert('True');
};
What's wrong with this:
Test if the value exists in the array
|
|||||||||
marked as duplicate by Phrogz, EJP, wallyk, cHao, Neil Knight Jul 11 '11 at 8:14This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. |
|||||||||
|
The Because it's an Array, the property names will be the indices of the Array. If you're only supporting a modern environment, you could use
If you need to support environments that don't have
|
|||||||||||||||||
|
Use an object instead. If this is all you're trying to do with the array, then an object is a much more efficient way to do a lookup list.
jsfiddle here to see it work: http://jsfiddle.net/jfriend00/ZNGTq/ |
||||
|
You need something like this:
See this for more info: http://snook.ca/archives/javascript/testing_for_a_v .... |
|||||||||||||||||||||
|
If you want to check if the array contains a given value, you can use the indexOf method to check for the position of an item. If the item is not found in the array, a -1 is returned:
See more at http://freewebdesigntutorials.com/javaScriptTutorials/jsStringObject/indexOfMethod.htm |
|||||
|
Because |
|||
|