Click to See Complete Forum and Search --> : search an array


PatrickLewis
01-09-2003, 10:27 AM
Is there a way to check if something is an element in an array?

Say:

a=4;
b= new Array(1,2,3,4,5,6);

Can i do something like:
if (a.elementOf(b)) {do this}

with out having to do a loop to check each element of the array?

swon
01-09-2003, 10:50 AM
Hi, array can be looped by:

a=4;
b= new Array(1,2,3,4,5,6);


for(i=0;i<b.length;i++){
if (b[i]==a){
do this
}
}

khalidali63
01-09-2003, 11:53 AM
No you can not do this in JavaScript.
JavaScript array does not support Java Collections functionality, since it does not have built in search algorithm.
therefor you will have to go through a loop and compare every object in the loop.

Khalid