javascript逗号分隔列表到数组

时间:2015-12-14 17:10:56

标签: javascript arrays json

我有来自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"]

任何人都知道如何获得这样的输出?

2 个答案:

答案 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)

拆分功能足以将字符串转换为数组;

appxmanifest

http://www.w3schools.com/jsref/jsref_split.asp