数值数组转字符串数组
方法一: 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]