我有来自javascript的这种类型的列表:
Amila,Asanka,Imaad,Kelum,Lakshan,Sagara,Thilina
我使用以下代码转换为我的输出
var array = columnsload.split(",");
var string = JSON.stringify(columnsload);
var nameArray = string.split(',');
输出如下:
"Amila,Asanka,Imaad,Kelum,Lakshan,Sagara,Thilina"
但我真的需要这样:
["Amila","Asanka","Imaad","Kelum","Lakshan","Sagara","Thilina"]
任何人都知道如何获得这样的输出?
答案 0 :(得分:0)
做var nameArray = columnsload.split(',');
。您不需要stringify
数组,然后再split
,只需一.split
即可。
var columnsload = "Amila,Asanka,Imaad,Kelum,Lakshan,Sagara,Thilina";
var nameArray = columnsload.split(',');
console.log(nameArray);
如果您需要将整个事物作为字符串,则可以在数组之后运行JSON.stringify
。
var columnsload = "Amila,Asanka,Imaad,Kelum,Lakshan,Sagara,Thilina";
var nameArray = columnsload.split(',');
console.log(JSON.stringify(nameArray));
// outputs ["Amila","Asanka","Imaad","Kelum","Lakshan","Sagara","Thilina"]
// as one string.
答案 1 :(得分:0)