我有一个带有数组数据的字符串变量。
var str = "[\r\n 10,\r\n 20\r\n]" ;
Run Code Online (Sandbox Code Playgroud)
我想像使用javascript一样将上述字符串转换为数组。
输出:-
var arr = [10,20];
Run Code Online (Sandbox Code Playgroud)
您可以简单地使用JSON.parse-它会忽略换行符,并将数组的字符串表示形式转换为JavaScript数组:
var str = "[\r\n 10,\r\n 20\r\n]" ;
var arr = JSON.parse(str);
console.log(Array.isArray(arr))
console.log(arr)Run Code Online (Sandbox Code Playgroud)