在 JavaScript 中将整数数组转换为字符串数组 [英] Convert integer array to string array in JavaScript
本文介绍了在 JavaScript 中将整数数组转换为字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的数组:
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.
推荐答案
您可以使用 map 并传递 String 构造函数作为一个函数,它将把每个数字变成一个字符串:
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']
这不会改变 sphValues.它将返回一个新数组.
This will not mutate sphValues. It will return a new array.
这篇关于在 JavaScript 中将整数数组转换为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文