JavaScript arrays: combining and splitting
(Page 1 of 4 )
This series of articles mainly concentrates on working with JavaScript arrays. This is the second article in the series and mainly concentrates on working with arrays effectively. You can reuse these scripts for injection into server side controls easily (especially in .NET and Java).
I already covered single dimensional and two dimensional arrays in my first article. If you are new to JavaScript arrays, I strongly suggest you go through that article. All of the examples in this series can be directly tested by copying and pasting the entire code (of each section) into any text file, saving it with the extension .HTM, and opening it using a browser.
How to combine or join all elements available in an array using JavaScript
Now, let us try to develop a simple script (JavaScript) to join all of the elements available in an array. Take a look at the following code:
<html>
<head>
<meta name=vs_targetSchema
content="http://schemas.microsoft.com/intellisense/ie5">
<script id="clientEventHandlersJS" language="javascript">
<!--
function Show()
{
var myArray = new Array();
myArray[0] = "Jag";
myArray[1] = "Chat";
myArray[2] = "Win";
myArray[3] = "Dhan";
var arrayAsString = myArray.join("<br>");
document.write(arrayAsString);
}
function Button1_onclick() {
Show();
}
//-->
</script>
</head>
<body>
<form id="form1">
<input type="button" value="Show" id="Button1" name="Button1" onclick="return Button1_onclick()">
</form>
</body>
</html>
Actually, within the above code, the “meta” tag is not necessary. Since I developed the above code using Visual Studio.NET 2003 Enterprise Architect, it was automatically added to provide its full-featured mechanisms.
Within the above code, I mainly created a simple button (which is identified as “Button1”). The button is defined with an “onclick” event which calls a JavaScript function, “Button1_onclick.” The same function simply calls another JavaScript function named “Show.”
The function “Show” is defined as follows:
function Show()
{
var myArray = new Array();
myArray[0] = "Jag";
myArray[1] = "Chat";
myArray[2] = "Win";
myArray[3] = "Dhan";
var arrayAsString = myArray.join("<br>");
document.write(arrayAsString);
}
The first statement creates an array named “myArray.” “Array()” is a built-in definition available in JavaScript language. You can create as many named arrays as possible (within a single web page). The immediate four statements in the above code place values in the array. The most important issue to concentrate on is the following statement:
var arrayAsString = myArray.join("<br>");
The above statement mainly concatenates all the elements of the array “myArray” and forms a single string. All the values of the array are separated with the “<BR>” tag (you can specify anything instead of <BR> tag). Finally, the string is assigned to “arrayAsString,” which is later displayed using the “document.write” statement.
Next: How to combine or join all elements available in a two-dimensional array using JavaScript >>
More JavaScript Articles
More By Jagadish Chaterjee