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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing String As An Array

Status
Not open for further replies.

snakehips2000

Programmer
Joined
Nov 10, 2003
Messages
95
Location
GB
I've got problems with the following JavaScript code. Although the array is seemingly created, I'm unable to access the individual elements (in this case, the first one 'uno'). Instead the browser displays the whole array, i.e. 'uno', 'dos', 'tres'. When I replace "document.writeln(arr2[0]);" with "document.writeln(arr2[1]);" I receive "undefined":

<script language = "Javascript">

function DuplicateRows(arry) {
var str=arry.substring(1, arry.length-1);
var arr2=new Array(str.split(","));
document.writeln(arr2[0]);
}

DuplicateRows("~'uno', 'dos', 'tres'~");
</script>

Any help much appreciated.
 
Hi

[tt]String.split()[/tt] returns an [tt]Array[/tt]. There is no need to call [tt]Array[/tt]'s constructor yourself.
Code:
var arr2=str.split(",");

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top