javascript 数组与字符串之间的转换

1、字符串转换为数组

let string = '123,456,789';
let stringResult = string.split(',');
console.log(stringResult) 
//输出["123", "456", "789"]
let string2 = 'abcdef'
let string2Result = string2.split('')
console.log(string2Result) //输出['a','b','c','d','e','f']
string2.split(",").map(Number);//输出[123,456,789]
JSON.parse("[" + string + "]"); //输出[123,456,789]

2、数组转换为字符串

let array = ['abc', 'def', 'hig']
let arrayResult = array.join(',')
console.log(arrayResult) // 输出"abc,def,hig"
array.toString()//输出"abc,def,hig"

 

posted @ 2020-04-18 20:35  必慎  阅读(324)  评论(0编辑  收藏  举报