Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help reading a list

Status
Not open for further replies.

TurboSRT4

Programmer
Nov 23, 2009
47
US
can someone please give me a simple example of creating a javascript list then checkin it for a value? I am fairly good with perl and heres is a perl example of what i need in javascript.

@list = ("one", "two", "three", "four");

foreach(@list){
if ($_ eq "one"){
$exist = "$_ exists in list";
}
}

thank you!
 
No Lists in JS,only Arrays.

Assuming its an array:

Code:
var myList=["one", "two", "three", "four"];
var x;
for(x in myList){
 if(myList[x]=='one'){
var exist='Exists in List';
 }

}
alert(exist);

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

Or abit different approach :
JavaScript:
[b]var[/b] myList[teal]=[[/teal][green][i]"one"[/i][/green][teal],[/teal] [green][i]"two"[/i][/green][teal],[/teal] [green][i]"three"[/i][/green][teal],[/teal] [green][i]"four"[/i][/green][teal]];[/teal]
[b]if[/b] [teal]([/teal]myList[teal].[/teal][COLOR=darkgoldenrod]indexOf[/color][teal]([/teal][green][i]'one'[/i][/green][teal])!=-[/teal][purple]1[/purple][teal])[/teal] exist[teal]=[/teal][green][i]'Exists in List'[/i][/green][teal];[/teal]
We recently discussed about [tt]Array.indexOf()[/tt] in thread216-1593118 ( see comments starting at 3 Mar 10 9:24 ).

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top