Best way to find if item is in array? | | |
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 use an
associative array/hash instead....
Is there a better/faster way?
I.e if I have a list of names, what's the best way to find out if the aray
contains "jane"?
--
J.B.Moreno | | | | re: Best way to find if item is in array?
J. B. Moreno wrote:[color=blue]
> 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 use an
> associative array/hash instead....
>
> Is there a better/faster way?
>
> I.e if I have a list of names, what's the best way to find out if the aray
> contains "jane"?
>[/color]
don't know if is better ?
alert(myArray.join.indexOf(myValue));
--
Stephane Moriaux et son [moins] vieux Mac | | | | re: Best way to find if item is in array?
ASM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> wrote:[color=blue]
> J. B. Moreno wrote:[color=green]
> > 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
> > use an associative array/hash instead....
> >
> > Is there a better/faster way?
> >
> > I.e if I have a list of names, what's the best way to find out if the
> > aray contains "jane"?
> >[/color]
>
> don't know if is better ?
>
> alert(myArray.join.indexOf(myValue));[/color]
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?
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.
Anyway, thanks for the alternative
--
J.B.Moreno | | | | re: Best way to find if item is in array?
"J. B. Moreno" <planb@newsreaders.com> wrote in message
news:20050831102305.819$nm@newsreader.com...[color=blue]
> ASM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> wrote:[color=green]
> > J. B. Moreno wrote:[color=darkred]
> > > 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
> > > use an associative array/hash instead....
> > >
> > > Is there a better/faster way?
> > >
> > > I.e if I have a list of names, what's the best way to find out if the
> > > aray contains "jane"?
> > >[/color]
> >
> > don't know if is better ?
> >
> > alert(myArray.join.indexOf(myValue));[/color]
>
> 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?
>
> 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.
>
> Anyway, thanks for the alternative
>
> --
> J.B.Moreno[/color]
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. | | | | re: Best way to find if item is in array?
J. B. Moreno wrote:[color=blue]
> ASM <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> wrote:
>[color=green]
>>don't know if is better ?
>>
>>alert(myArray.join.indexOf(myValue));[/color][/color]
error ! have had to read :
alert(myArray.join().indexOf(myValue));
[color=blue]
> 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?[/color]
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');
[color=blue]
> 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.[/color]
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',ba nana');
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>
--
Stephane Moriaux et son [moins] vieux Mac | | | | re: Best way to find if item is in array?
"McKirahan" <News@McKirahan.com> wrote in message
news:OIWdnZ2dnZ1bMUOQnZ2dnZ5ciN6dnZ2dRVn-z52dnZ0@comcast.com...
<snip>
[color=blue]
> 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.[/color]
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 | | | | re: Best way to find if item is in array?
Jim Davis wrote:[color=blue]
>
> 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. ;^)[/color]
tbl = new Array("frank", "jim'n'jane", "moriaux", "toto", "foo");
alert(tbl['toto']);
returns : undefined
--
Stephane Moriaux et son [moins] vieux Mac | | | | re: Best way to find if item is in array?
ASM wrote on 31 aug 2005 in comp.lang.javascript:
[color=blue]
> Jim Davis wrote:[color=green]
>>
>> 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. ;^)[/color]
>
> tbl = new Array("frank", "jim'n'jane", "moriaux", "toto", "foo");
>
> alert(tbl['toto']);
>
> returns : undefined
>
>[/color]
var tbl = {"frank":1, "jim'n'jane":1,
"moriaux":1, "toto":1, "foo":1};
alert(tbl['toto']); // 1
alert(tbl['blahblah']); // undefined
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress) | | | | re: Best way to find if item is in array?
"ASM" <stephanemoriaux.NoAdmin@wanadoo.fr.invalid> wrote in message
news:43160ef9$0$1715$8fcfb975@news.wanadoo.fr...[color=blue]
> Jim Davis wrote:[color=green]
>>
>> 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. ;^)[/color]
>
> tbl = new Array("frank", "jim'n'jane", "moriaux", "toto", "foo");
>
> alert(tbl['toto']);
>
> returns : undefined[/color]
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 |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|