admin管理员组文章数量:1134571
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.
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.
Share Improve this question edited Oct 4, 2020 at 13:44 Penny Liu 17.3k5 gold badges86 silver badges108 bronze badges asked Oct 29, 2014 at 6:43 gihansalithgihansalith 1,9205 gold badges20 silver badges21 bronze badges 4 |10 Answers
Reset to default 310You 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.
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"]
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.
for(var i = 0; i < sphValues.length; i += 1){
sphValues[i] = '' + sphValues[i];
}
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();
}
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}`);
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];
}
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"]
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' ]
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']
本文标签: Convert integer array to string array in JavaScriptStack Overflow
版权声明:本文标题:Convert integer array to string array in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736766553a1951852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
sphValues.map(String)
– elclanrs Commented Oct 29, 2014 at 6:45[1,2,3,4,5].toString().split(",")
– Mr_Green Commented Oct 29, 2014 at 6:48