数组转字符串
数组方法 | 说明 |
to String() | 将数组转换成一个字符串 |
join() | 将数组元素连接起来构建一个字符串 |
toString()方法:
let a = [1, 2, 3, 4, 5]
console.log(a) //[1, 2, 3, 4, 5]
let b = a.toString()
console.log(b) //1, 2, 3, 4, 5
join()方法:
let a = [1, 2, 3, 4, 5]
console.log(a) //[1, 2, 3, 4, 5]
let b = a.join() //括号里面可以写分隔符,如果不写默认是逗号
console.log(b) //1, 2, 3, 4, 5
2.字符串转数组
split()方法:
let a = '111, 222, 333'
console.log(a) //111, 222, 333
let arr = a.split(',') //从每一个逗号处开始分割
console.log(arr) //['111', '222', '333']