数据格式转换的操作

1.字符串转为数组

let str = 'helloworld'
let tempStr = str.split('') 
let arrStr = [...str]
console.log(tempStr) //["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
console.log(arrStr) //["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]

2.数组转字符串

let arrList = ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
let stringOne = arrList.join('')
console.log(stringOne) //helloworld
toString将数组的每个元素都转换为字符串。当每个元素都被转换为字符串时,使用逗号进行分隔,以列表的形式输出这些字符串。
let arrList = ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
let stringTwo = arrList.toString()
console.log(stringTwo) //h,e,l,l,o,w,o,r,l,d
// 全局替换
let tempString = stringTwo.replace(/,/g,'')
console.log(tempString) //helloworld

  

原文地址:https://www.cnblogs.com/cuipingzhao/p/15318840.html