在对接的过程中,后台传来的数据其中有一个是数组字符串,这时候就需要解析成纯数组了。
使用JSON.parse()
可以解析
let str = '["abc", "def"]'
console.log(str)
string = JSON.parse(str)
console.log(str)
// ["abc", "def"] // 字符串
// (2) ["abc", "def"] // 数组
也可以使用eval()
进行解析
let str = "['abc', 'def']"
console.log(str)
string = eval(str)
console.log(str)
// ["abc", "def"] // 字符串
// (2) ["abc", "def"] // 数组