Reputation: 3
var categoryList="[Service,Ticket,Bill,Entertainment,Restaurant]";
I want to convert the above String as String array like below:-
var categoryList=["Service","Ticket","Bill","Entertainment","Restaurant"];
Any Solution is appreciable.
Upvotes: 0
Views: 48
Reputation: 115222
You can use slice()
and 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
)
Upvotes: 1
Reputation: 92854
Another short solution using String.match
function:
var categoryList="[Service,Ticket,Bill,Entertainment,Restaurant]";
console.log(categoryList.match(/\w+\b/g)); // ["Service", "Ticket", "Bill", "Entertainment", "Restaurant"]
Upvotes: 0
Reputation: 4623
var yourString = "[Service,Ticket,Bill,Entertainment,Restaurant]"
var categoryList = yourString.substring(1, yourString.length-1).split(",") // removing [] and splitting with `,`
Upvotes: 0