Tim Tom
Tim Tom

Reputation: 2162

How to convert this string to some array?

I am using one 3rd party plugin which uses stringify and gives me something like:

["ProjectB","ProjectA","Paris"]

It was an array but it used stringify and serialized into this format.How do I get back my array from this? Now I could very well use split and then remove 1st and last character from every string and get it but I don't want to do that manually. Is that any built in utility that can do that for me?

Upvotes: 0

Views: 53

Answers (2)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Assuming you have like var str = '["ProjectB","ProjectA","Paris"]';

Try using,

var array = JSON.parse(str); //will return you an array

As @AlexMA pointed out: JSON.parse is not supported in old browsers so you are better off using jQuery version like below,

var array = $.parseJSON(str);

Upvotes: 5

thecodeparadox
thecodeparadox

Reputation: 87073

You can use

JSON.parse(your_arr_str);

or jquery

$.parseJSON(your_arr_str);

Upvotes: 0

Related Questions