Open In App

Implode an array with jQuery/JavaScript

Last Updated : 31 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of elements and the task is to implode (join the elements of an array) the array elements.

Method 1: Using join() method: The join() method can be used to join an array with a separator and return it as a string. The join() method takes in the optional parameter of a separator. The separator is assumed to be a comma, if not specified. If the array contains only one string, then it would be returned without the separator.

Syntax:

array.join(separator)

Example:




<!DOCTYPE html>
<html>
      
<head>
    <title>
        Implode an array with jQuery/Javascript?
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        Implode an array with jQuery/Javascript? 
    </b>
      
    <p>
        Original array is ["One",
        "Two", "Three", "Four", "Five"]
    </p>
      
    <p>
        Imploded array is: 
        <span class="output"></span>
    </p>
  
    <button onclick="implodeArray()">
        Implode Array
    </button>
      
    <script type="text/javascript">
      
        function implodeArray() {
            originalArray =
                ["One", "Two", "Three", "Four", "Five"];
          
            separator = ' ';
            implodedArray = originalArray.join(separator);
  
            console.log(implodedArray);
            document.querySelector('.output').textContent
                    = implodedArray;
        }
    </script>
</body>
  
</html>                    


Output:

  • Before clicking the button:
  • After clicking the button:
    join-output
  • Console Output:
    join-console

Method 2: Iterating through the array and create a new string with all the strings and concatenated separators: The implode function can be created by looping through the array and concatenating it to one base string. The separator is added after concatenating each string unless it is the last string in the array. This prevents from adding the separator to the end of the last string in the array.
This method is slower than the join() method as a large number of strings are temporarily created during the concatenation of the base string.

Example:




<!DOCTYPE html>
<html>
      
<head>
    <title>
        Implode an array with jQuery/Javascript? 
    </title>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>
        Implode an array with jQuery/Javascript? 
    </b>
      
    <p>
        Original array is ["One",
        "Two", "Three", "Four", "Five"]
    </p>
      
    <p>
        Imploded array is: 
        <span class="output"></span>
    </p>
  
    <button onclick="implodeArray()">
        Implode Array
    </button>
      
    <script type="text/javascript">
      
        function implodeArray() {
            originalArray =
            ["One", "Two", "Three", "Four", "Five"];
              
            separator = '+';
            implodedArray = '';
  
            for(let i = 0; i < originalArray.length; i++) {
  
                // add a string from original array
                implodedArray += originalArray[i];
  
                // unless the iterator reaches the end of
                // the array add the separator string
                if(i != originalArray.length - 1){
                    implodedArray += separator; 
                }
            }
  
            console.log(implodedArray);
            document.querySelector('.output').textContent
                    = implodedArray;
        }
    </script>
</body>
  
</html>                    


Output:

  • Before clicking the button:
  • After clicking the button:
    js-output
  • Console Output:
    js-console


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads