其他分享
首页 > 其他分享> > 数值数组与字符串数组转换

数值数组与字符串数组转换

作者:互联网

数值数组转字符串数组

方法一:
var arr1 = [1, 2, 5];
arr1 = arr1.map(String);  //将arr1转换为字符串数组
console.log(arr1);//结果: ["1","2","5"]
方法二: var arr1 = [1, 2, 5]; arr1 = arr1.toString(); //将arr1转换为字符串,结果为:"1,2,5" arr1=arr1.split(",");//将arr1转换为数组,分隔符为",",结果为["1", "2", "5"]

字符串数组转数值数组

方法一:
var arr1 = ["1","2","5"];
arr1 = arr1.map(Number);  //将arr1转换为数值数组
console.log(arr1);//结果: [1,2,5]

方法二:
var arr1 = ["1","2","5"];
for(let i=0;i<arr1.length;i++){
    arr1[i] = parseInt(arr1[i])
}        
console.log(arr1);// 结果为:[1,2,5]

 

标签:转换,数组,数值,var,arr1,字符串
来源: https://www.cnblogs.com/zhaoshuxin/p/16639840.html