簡體   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