> > What's the best (i.e. fastest) way to find out if an array contains a
> > given value? Other than looping, the only way I know to do it is to
[quoted text clipped - 8 lines]
>
> alert(myArray.join.indexOf(myValue));
> > > What's the best (i.e. fastest) way to find out if an array contains a
> > > given value? Other than looping, the only way I know to do it is to
[quoted text clipped - 22 lines]
> --
> J.B.Moreno
Change
alert(myArray.join.indexOf("jane"));
to
alert(myArray.join().indexOf("jane"));
To ensure that your search string is not embedded try this:
var myJoin = "|" + myArray.join("|") + "|";
alert(myJoin.indexOf("|frank|"));
alert(myJoin.indexOf("|jane|"));
This presumes that "|" is not a character in your array.
Jim Davis - 31 Aug 2005 18:42 GMT
<snip>
> Change
> alert(myArray.join.indexOf("jane"));
[quoted text clipped - 8 lines]
>
> This presumes that "|" is not a character in your array.
In general, unless your array is very large or you search it a lot, a
client-side loop is just so damn fast why bother playing games?
And if it IS very large or you search it a lot why not set up a hash table?
Then it's just "if ( myHash["Jane"] ) {};" Instant results with no fuss.
Just my opinion of course - which doesn't change the fact that McKirahan's
solution will, indeed, work just fine. ;^)
Jim Davis
ASM - 31 Aug 2005 21:09 GMT
> And if it IS very large or you search it a lot why not set up a hash table?
> Then it's just "if ( myHash["Jane"] ) {};" Instant results with no fuss.
>
> Just my opinion of course - which doesn't change the fact that McKirahan's
> solution will, indeed, work just fine. ;^)
tbl = new Array("frank", "jim'n'jane", "moriaux", "toto", "foo");
alert(tbl['toto']);
returns : undefined

Signature
Stephane Moriaux et son [moins] vieux Mac
Evertjan. - 31 Aug 2005 21:38 GMT
ASM wrote on 31 aug 2005 in comp.lang.javascript:
>> And if it IS very large or you search it a lot why not set up a hash
>> table? Then it's just "if ( myHash["Jane"] ) {};" Instant results
[quoted text clipped - 8 lines]
>
> returns : undefined
var tbl = {"frank":1, "jim'n'jane":1,
"moriaux":1, "toto":1, "foo":1};
alert(tbl['toto']); // 1
alert(tbl['blahblah']); // undefined

Signature
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)
Jim Davis - 31 Aug 2005 22:41 GMT
>> And if it IS very large or you search it a lot why not set up a hash
>> table? Then it's just "if ( myHash["Jane"] ) {};" Instant results with
[quoted text clipped - 8 lines]
>
> returns : undefined
I'm sorry... I think you've missed something. This isn't a hash table so it
would, of course, return "undefined".
A "normal" array, as you've built, is indexed by a count (numbers) -
1,2,3,4,5,etc. You can access any element by it's numerical position in the
array (JavaScript also supports numerically indexed arrays where the numbers
are not in order, but leave them out of the discussion for now). You can do
"MyArray[1]", "MyArray[4]", etc. You can loop over the array easily using a
counter loop.
A hash table can be most easily thought (at least by me) of an array indexed
by values (the values can be anything: strings, object references,
whatever). In many languages these values are called "keys" and in
JavaScript they're object properties. You would access values by
"MyHash[MyKeyValue]" (just as you would access any object property using
bracket notation).
So, to demonstrate, try this:
tbl = {"frank": null, "jim'n'jane": null, "moriaux": null, "toto": null,
"foo": null}
This (object literal notation) will create an object where each property is
the "name" and the value is "null". If you need access to array methods in
your hash (most of the time you don't) you might also do this:
tbl = new Array();
tbl["frank"] = null;
tbl["jim'n'jane"] = null;
tbl["moriaux"] = null;
tbl["toto"] = null;
tbl["foo"] = null;
(But, as noted elsewhere, you cannot use any existing array properties -
namely "length" as a key name.)
In either case however try alert(tbl['toto']); again.
This is a very powerful aspect of the language that just doesn't get use "in
the wild".
Jim Davis
>>don't know if is better ?
>>
>>alert(myArray.join.indexOf(myValue));
error ! have had to read :
alert(myArray.join().indexOf(myValue));
> I think I understand that, but let me test my understanding:
> It joins the array into a string, and then searches inside the string
> for the given value? So, if for instance I had myArray = new
> Array("frank", "jim'n'jane"); and did
> alert(myArray.join.indexOf("jane")); it would find it?
no
if it finds it returns '1'
if not it returns '-1'
so for true/false :
alert(myArray.join().indexOf(myValue)>=0)
that's to say :
if(myArray.join().indexOf(myValue)>=0) alert('true');
else alert('false');
> That's not actually a show stopper in this case...but I might have to do
> some test to see if it's actually faster.
a demo ?
<html>
<script type="text/javascript">
tbl = new Array("frank", "jim'n'jane", "moriaux", "toto", "foo");
arr = new Array('potato','salade','pear','orange','apple',banana');
alert(tbl.join().indexOf('ri')>=0);
alert(tbl.join().indexOf('st')>=0);
alert('results must have been :\n true\nfalse');
function verif(myArray,myValue) {
var yesno = eval(myArray).join().indexOf(myValue)>=0;
return yesno;
}
document.write('array = "tbl"<br>content = '+tbl.join()+
'<br>array = arr<br>'+arr.join());
</script>
<form onsubmit="alert(verif(I1.value,I2.value));return false;">
<p>Enter array's name : <input name="I1" value="tbl">
Enter search value : <input name="I2" value="">
<hr><p align=center><input type=submit value="verify">
<input type=reset value="reset">
</form>
</html>

Signature
Stephane Moriaux et son [moins] vieux Mac