0
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.

1
  • categoryList.substring(1, categoryList.length - 1).split(',') Commented May 22, 2016 at 10:44

4 Answers 4

1

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
)

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

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

Comments

0

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.