How to convert Integer array to String array using JavaScript ?
The task is to convert an integer array to a string array. Here are a few of the most used techniques discussed with the help of JavaScript. In the first approach we are going to use the .toString() method with the .map() method and in the second approach we will use the .join() method with the .split() method.
Approach 1: In this approach, we use the .toString() method on every element of the array with the help of the .map() method.
Example: This example uses the approach discussed above.
html
< body style = "text-align:center" > < h1 style = "color:green" > GeeksforGeeks </ h1 > < p id = "GFG" ></ p > < button onclick = "gfg_Run()" > Click Here </ button > < p id = "geeks" style = "font-weight:bold" ></ p > < script > var el_up = document.getElementById("GFG"); var el_down = document.getElementById("geeks"); var arr = [1, 4, 56, 43, 67, 98]; el_up.innerHTML = "Click on the button to " + "convert the integer array" + " to string array.< br >Array = '" + arr + "' typeof(Array[0]) - " + typeof(arr[0]); function gfg_Run() { var gfg = arr.map(function(e){ return e.toString() }); el_down.innerHTML = "Array - " + gfg + " typeof(Array[0]) - " + typeof(gfg[0]); } </ script > </ body > |
Output:
Approach 2: In this approach, we use the join() method which joins the array and returns it as a string. Then split() method splits the string on “, ” returned by the join() method.
Example: This example uses the approach discussed above.
html
< body style = "text-align:center" > < h1 style = "color:green" >GeeksforGeeks</ h1 > < p id = "GFG" ></ p > < button onclick = "gfg_Run()" > Click Here </ button > < p id = "geeks" style = "font-weight:bold" ></ p > < script > var el_up = document.getElementById("GFG"); var el_down = document.getElementById("geeks"); var arr = [1, 4, 56, 43, 67, 98]; el_up.innerHTML = "Click on the button to " + "convert the integer array" + " to string array.< br >Array = '" + arr + "' typeof(Array[0]) - " + typeof(arr[0]); function gfg_Run() { var gfg = arr.join().split(', '); el_down.innerHTML = "Array - " + gfg + " typeof(Array[0]) - " + typeof(gfg[0]); } </ script > </ body > |
Output:
Please Login to comment...