Convert integer array to string array in JavaScript

JavascriptArrays

Javascript Problem Overview


I have an array like below:

var sphValues = [1, 2, 3, 4, 5];

then I need to convert above array like as below one:

var sphValues = ['1', '2', '3', '4', '5'];

How can I convert? I used this for auto-complete.

Javascript Solutions


Solution 1 - Javascript

You can use map and pass the String constructor as a function, which will turn each number into a string:

sphValues.map(String) //=> ['1','2','3','4','5']

This will not mutate sphValues. It will return a new array.

Solution 2 - Javascript

just by using array methods

var sphValues = [1,2,3,4,5];   // [1,2,3,4,5] 
sphValues.join().split(',')    // ["1", "2", "3", "4", "5"]

Solution 3 - Javascript

Use Array.map:

var arr = [1,2,3,4,5];
var strArr = arr.map(function(e){return e.toString()});
console.log(strArr); //["1", "2", "3", "4", "5"] 

Edit:
Better to use arr.map(String); as @elclanrs mentioned in the comments.

Solution 4 - Javascript

for(var i = 0; i < sphValues.length; i += 1){
    sphValues[i] = '' + sphValues[i];
}

Solution 5 - Javascript

Use .map() at this context that is a better move, as well as you can do like the below code this would add more readability to your code,

sphValues.map(convertAsString);

function convertAsString(val) {
  return val.toString();
}

Solution 6 - Javascript

you can just append a '' to convert it to a string type.

var sphValues = [1,2,3,4,5];
for(var itr = 0; itr<sphValues.length;itr++){
  sphValues[itr] = '' + sphValues[itr];
}

Solution 7 - Javascript

 var value;
 for (var i = 0; i < data3.sph.length; i++) {
     value = data3.sph[i];
     //sphValues[i] = data3.sph[i];
     var obj = {
         label: value
     };
     sphValues.push(obj);
 }

You can use this method for auto complete. I think your problem will be solved, but it will not convert like you want, it will convert like

["label": "val1", "label": "val2"]

Solution 8 - Javascript

ES6 solution

const nums = [1, 2, 3, 4, 5, 10]; // bugfix: the 10 edge case const strs = Array.from(nums, x => ${x}); console.log(strs); // ['1', '2', '3', '4', '5', '10']

> git diff

-  const strs = Array.from(nums.join(``));
+  const strs = Array.from(nums, x => `${x}`);

Solution 9 - Javascript

Create a function that takes an array of integers and strings. Convert integers to strings.

function parseArray(arr) {
	 const str = arr.toString();
     const backToArr = str.split(',');
     return backToArr;
}

Solution 10 - Javascript

Another solution using map:

let nums = [1,2,2,4,3,2,5,6]
let all_to_str = nums.map(num => {return num.toString()})
console.log(all_to_str)

Output:

> [ '1', '2', '2', '4', '3', '2', '5', '6' ]

Solution 11 - Javascript

just by using the array.map method, you can convert an array of numbers into an array of string

var sphValues = [1, 2, 3, 4, 5];
const numberToStringArray = sphValues.map((number) => {
return number.toString();
});
console.log(numberToStringArray); //['1', '2', '3', '4', '5']

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongihansalithView Question on Stackoverflow
Solution 1 - JavascriptelclanrsView Answer on Stackoverflow
Solution 2 - JavascriptSkiper SkiprovicView Answer on Stackoverflow
Solution 3 - JavascriptAmir PopovichView Answer on Stackoverflow
Solution 4 - JavascriptAnarionView Answer on Stackoverflow
Solution 5 - JavascriptRajaprabhu AravindasamyView Answer on Stackoverflow
Solution 6 - JavascriptEkansh RastogiView Answer on Stackoverflow
Solution 7 - JavascriptSarangaView Answer on Stackoverflow
Solution 8 - JavascriptxgqfrmsView Answer on Stackoverflow
Solution 9 - JavascriptS GabaleView Answer on Stackoverflow
Solution 10 - JavascriptArindam RoychowdhuryView Answer on Stackoverflow
Solution 11 - JavascriptTayyabView Answer on Stackoverflow