JavaScript
  Home arrow JavaScript arrow JavaScript arrays: combining and splitting
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript arrays: combining and splitting
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-03-07

    Table of Contents:
  • JavaScript arrays: combining and splitting
  • How to combine or join all elements available in a two-dimensional array using JavaScript
  • How to split a string into an array using JavaScript
  • How to split a sentence into words using JavaScript

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More JavaScript Articles
    More By Jagadish Chaterjee


       · Hello, I introduced joining and splitting arrays in javascript in this article....
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT