1.数组转化成字符串:
let arr = [1, 2, 3, 4, 5];
let str = arr.join(",");
console.log(str); // 输出 "1,2,3,4,5"
2.字符串转化成数组
let str = "1,2,3,4,5";
let arr = str.split(",");
console.log(arr); // 输出 ["1", "2", "3", "4", "5"]
3.数组字符串转化为数组数字
方法一:
let arr = ['1', '2', '3', '4', '5'];
// 使用map()方法将数组中的每个元素转换为数字
let numArr = arr.map(item => Number(item));
console.log(numArr); // 输出 [1, 2, 3, 4, 5]
方法二:
let strArray = ['1', '2', '3', '4', '5'];
let arr = Array.from(strArray);
console.log(arr); // 输出 [1, 2, 3, 4, 5]
方法三:
let strArray = ['1', '2', '3', '4', '5'];
let arr = Array.from(strArray).map(item => Number(item));
console.log(arr); // 输出 [1, 2, 3, 4, 5]