繁体   English   中英

如何将列表元素转换为字符串数组

[英]How to convert list elements to string array

var categoryList="[Service,Ticket,Bill,Entertainment,Restaurant]";

我想将上面的String转换为String数组,如下所示:-

var categoryList=["Service","Ticket","Bill","Entertainment","Restaurant"];

任何解决方案都是可观的。

categoryList = categoryList.replace(/\[|\]/g,"").split(",");

您可以使用slice()split()

 var categoryList = "[Service,Ticket,Bill,Entertainment,Restaurant]" .slice(1, -1) // get `[` and `]`removed string .split(','); // split based on comma to get array console.log( categoryList ) 

var yourString = "[Service,Ticket,Bill,Entertainment,Restaurant]"

var categoryList = yourString.substring(1, yourString.length-1).split(",") // removing [] and splitting with `,`

使用String.match函数的另一个简短解决方案:

var categoryList="[Service,Ticket,Bill,Entertainment,Restaurant]";
console.log(categoryList.match(/\w+\b/g));  // ["Service", "Ticket", "Bill", "Entertainment", "Restaurant"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM