138

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.

Penny Liu
18k5 gold badges89 silver badges109 bronze badges
asked Oct 29, 2014 at 6:43
4
  • 37
    sphValues.map(String) Commented Oct 29, 2014 at 6:45
  • 8
    @elclanrs You should post that as an answer and not a comment Commented Oct 29, 2014 at 6:47
  • 12
    [1,2,3,4,5].toString().split(",") Commented 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 Commented Oct 29, 2014 at 6:49

10 Answers 10

314

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.

Frazer Kirkman
1,2041 gold badge16 silver badges25 bronze badges
answered Oct 29, 2014 at 6:51
Sign up to request clarification or add additional context in comments.

2 Comments

For older browsers which do not support Array.map, you can use underscore.js: _.map(sphValues, String)
Unfortunately, this will also convert booleans to strings.
17

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"]
answered Mar 10, 2016 at 0:03

Comments

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.

answered Oct 29, 2014 at 6:49

1 Comment

Why and not just toString() ?
6
for(var i = 0; i < sphValues.length; i += 1){
 sphValues[i] = '' + sphValues[i];
}
answered Oct 29, 2014 at 6:45

Comments

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();
}
answered Oct 29, 2014 at 6:55

Comments

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}`);
answered Jul 31, 2020 at 9:18

Comments

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];
}
answered Oct 29, 2014 at 6:46

Comments

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"]
MarsOne
2,1875 gold badges30 silver badges54 bronze badges
answered Oct 29, 2014 at 7:04

Comments

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

answered Feb 6, 2021 at 20:08

Comments

0

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']
answered Oct 21, 2021 at 18:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.