123

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.

4
  • 35
    sphValues.map(String)
    – elclanrs
    Oct 29, 2014 at 6:45
  • 8
    @elclanrs You should post that as an answer and not a comment
    – Mr. Alien
    Oct 29, 2014 at 6:47
  • 10
    [1,2,3,4,5].toString().split(",")
    – Mr_Green
    Oct 29, 2014 at 6:48
  • 1
    @SonalPM You should refer markdown as you are having a hard time to post a comment with a link, cuz you posted and deleted your comment 4 times, now 5
    – Mr. Alien
    Oct 29, 2014 at 6:49

11 Answers 11

281

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.

2
  • 5
    For older browsers which do not support Array.map, you can use underscore.js: _.map(sphValues, String) Jul 8, 2015 at 9:35
  • Unfortunately, this will also convert booleans to strings.
    – Dev
    Nov 13, 2020 at 3:40
16

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"]
0
13

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.

1
  • Why and not just toString() ?
    – Mah Neh
    Oct 14, 2022 at 14:49
6
for(var i = 0; i < sphValues.length; i += 1){
    sphValues[i] = '' + sphValues[i];
}
6

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();
}
5

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}`);
2
  • This is wrong. If you have a value e.g. 10, it will output as '1', '0'
    – chitgoks
    Apr 7, 2022 at 2:28
  • @chitgoks You're are right, now I had fixed it.
    – xgqfrms
    Apr 7, 2022 at 15:14
3

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];
}
3
 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"]
-1

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' ]

-1

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']
-1

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.