在JavaScript中,可以使用多种方法将字符串分割成数组和数组转字符串

1、使用 split() 方法:

let str = "Hello, World!";
let arr = str.split(" "); // ["Hello,", "World!"]

2、使用扩展运算符(…)和 split() 方法:

let str = "Hello, World!";
let arr = [...str.split(" ")]; // ["Hello,", "World!"]

3、使用 Array.from() 方法:

let str = "Hello, World!";
let arr = Array.from(str, function(char) { return char; }); 
// ["H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d", "!"]

4、数组转字符串

const array = ['apple', 'banana', 'orange'];
const string = array.join(', '); // 使用逗号和空格作为分隔符
console.log(string); // 输出 "apple, banana, orange"

你可能感兴趣的:(javascript,前端)