javascript 将字符串数组表示形式转换回数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10003706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:27:40  来源:igfitidea点击:

Convert string array representation back to an array

javascript

提问by Chin

I have a string representation of an array:

我有一个数组的字符串表示:

'["item1", "item2", "item3"]'

I'm trying to convert this back to an array.

我正在尝试将其转换回数组。

Any pointers appreciated.

任何指针表示赞赏。

回答by stewe

Just use JSON.parse('["item1", "item2", "item3"]');

只需使用 JSON.parse('["item1", "item2", "item3"]');

回答by georg

Try JSON.parse:

试试JSON.parse

 ary = JSON.parse(s);

Most browsers support it natively, for others you'll need json.js

大多数浏览器本身就支持它,对于其他浏览器,您需要json.js

回答by Yogesh Prajapati

You also can Use

你也可以使用

var genericList = eval('(' + s + ')');

for(i=0;i<genericList.lengthl;i++){
   alert("element : " + genericList[i]);
}