Implode an array with jQuery/JavaScript - GeeksforGeeks

31 May, 2019
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:
Console Output:
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:
Console Output:
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills