[英]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(",");
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.