Best way to find if item is in array?

J

J. B. Moreno

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"?
 
A

ASM

J. B. Moreno said:
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"?

don't know if is better ?

alert(myArray.join.indexOf(myValue));
 
J

J. B. Moreno

ASM said:
don't know if is better ?

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?

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
 
M

McKirahan

J. B. Moreno said:
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

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.
 
A

ASM

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>
 
J

Jim Davis

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.

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
 
A

ASM

Jim said:
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
 
E

Evertjan.

ASM wrote on 31 aug 2005 in comp.lang.javascript:
Jim said:
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

var tbl = {"frank":1, "jim'n'jane":1,
"moriaux":1, "toto":1, "foo":1};

alert(tbl['toto']); // 1

alert(tbl['blahblah']); // undefined
 
J

Jim Davis

ASM said:
Jim said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top