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.
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.
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.
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"]
sphValues.map(String)[1,2,3,4,5].toString().split(",")