Array.prototype.in_array = function ( obj ) {
var len = this.length;
for ( var x = 0 ; x <= len ; x++ ) {
if ( this[x] == obj ) return true;
}
return false;
}
numbers = new Array("one","two","three","four","five");
alert(numbers.in_array("three")) // should evaluate to true
<script type="text/javascript">
numbers = new Array("one","two","three","four","five");
if (numbers.toString().indexOf('three')!==-1)
alert('Three is in numbers!')
</script>
<script type="text/javascript">
numbers = new Array("one","two","three","four","five");
if (numbers.toString().indexOf('three')!==-1)
alert('Three is in numbers!')
</script>
That's the method I would have gone for.
Incidentally, is !== -1 any different to != -1 ?
I ask as I'm more familiar with the latter (!= -1), but the former (!== -1) doesn't throw an error, so I'm wondering…
<script type="text/javascript">
numbers = new Array("one","two","three","four","five");
if (numbers.toString().indexOf('three')!==-1)
alert('Three is in numbers!')
</script>
FWIW: The above will allow thre to pass as well as three...
Code:
<script type="text/javascript">
var numbers = new Array("one","two","three","four","five");
Array.prototype.in_array = function(obj){
return new RegExp('(^|\,)'+obj+'(\,|$)','gi').test(this);
}
alert(numbers.in_array("two")) // should evaluate to true
</script>
I did not create the strict, but for the rest of it I coded to the page he provided. You can input objects, strings, numbers, arrays, what ever your heart desires. It will search for it. Not just strings and numbers.
Last edited by SpirtOfGrandeur; 07-18-2005 at 05:09 PM..
Since it seems you're trying to write an equiv. to the PHP version, you may want to also index associative arrays:
Code:
Object.prototype.in_array = function(datum, strict) {
if (strict) function equals(a,b) { return a === b }
else function equals(a,b) { return a == b }
for (var i in this) {
if (equals(this[i], datum)) return true;
}
return false;
}
toString() on an array is still an O(n) operation, so you don't speed up anything at all, and in fact, you might slow it down giving the complexity of string concatenation. Using this version of in_array, you would do:
Willy, I don't know on what planet 'thre'=='three' but, here on earth and in IE6 , NS7.2 and FF they don't and my script works without mistaking 'thre' for 'three'. It will however mistake 'threee' for 'three', except for this slight mod:
Code:
<script type="text/javascript">
numbers = new Array("one","two","three","four","five");
if ((','+numbers.toString()+',').indexOf(',three,')!==-1)
alert('Three is in numbers!')
</script>
which will prevent that error.
Bill, I've just gotten into the habit of always using == in conditionals. I saw somewhere that it is called for (certainly for testing equal) in some cases even when testing for not equal. The distinction seemed hard to remember but, since it also works when the more common != does, I figured - what will it hurt?
Last edited by jscheuer1; 07-19-2005 at 05:46 AM..
Willy, I don't know on what planet 'thre'=='three' but, here on earth and in IE6 , NS7.2 and FF they don't and my script works without mistaking 'thre' for 'three'.
On this planet... The planet earth... Third rock from the sun...
Try to get your head out of the clouds and ground yourself...
Did you try running your script searching for thre??
Here, run this and let me know what happens...
Your script throws the alert...
Thus, thre==three...
Code:
<script type="text/javascript">
numbers = new Array("one","two","three","four","five");
if (numbers.toString().indexOf('thre')!==-1)
alert('Three is in numbers!')
</script>
.....Willy
Last edited by Willy Duitt; 07-19-2005 at 08:48 AM..
BTW: Your planets must not be properly aligned...
Your latest revision is even more problematic...
Code:
<script type="text/javascript">
numbers = new Array("one","two","three","four","five");
if ((','+numbers.toString()+',').indexOf(',three,')!==-1)
alert('Three is in numbers!')
</script>
Your inclusion of the comma's before and after assumes that the string to match will appear in the middle between the beginning and the end of the array... Try searching for one or five...
Yeah, about the thre/three thing. I thought you were putting 'thre' in the array not in the index search. In any case the new method takes care of all eventualities so far mentioned and is still quite simple compared to parsing out the array contents with a 'for i' loop.
I haven't read the in_array spec but...
I do not see the need to loop thru the array if, as the O/P originally requested, the intention is to merely check for a match that the search data is present in the array...
However, if the intention is to match and manipulate the array in some way such as using push, pop, splice or slice... I would agree that looping thru the array would be benificial since it will provide the index to the match's place in the array for further manipulation...