Home arrow JavaScript arrow JavaScript arrays: combining and splitting
JAVASCRIPT

JavaScript arrays: combining and splitting


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

Author Info:
By: Jagadish Chaterjee
Rating: 5 stars5 stars5 stars5 stars5 stars / 21
March 07, 2006
TABLE OF CONTENTS:
  1. · JavaScript arrays: combining and splitting
  2. · How to combine or join all elements available in a two-dimensional array using JavaScript
  3. · How to split a string into an array using JavaScript
  4. · How to split a sentence into words using JavaScript

print this article
SEARCH DEVARTICLES

JavaScript arrays: combining and splitting
(Page 1 of 4 )

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials