Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string array representation back to an array

Tags:

javascript

I have a string representation of an array:

'["item1", "item2", "item3"]'

I'm trying to convert this back to an array.

Any pointers appreciated.

like image 621
Chin Avatar asked Apr 04 '12 01:04

Chin


People also ask

How do I turn a string back into an array?

The String. split() method converts a string into an array of substrings using a separator and returns a new array. It splits the string every time it matches against the given separator. You can also optionally pass in an integer as a second parameter to specify the number of splits.

How do I turn an array into a string array?

To convert an array of numbers to an array of strings, call the map() method on the array, and on each iteration, convert the number to a string. The map method will return a new array containing only strings.

How do I convert a string to an array of characters?

String str = "Tutorial"; Now, use the toCharArray() method to convert string to char array. char[] ch = str. toCharArray();

How do I convert a string to an array in JavaScript?

The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.


2 Answers

Just use JSON.parse('["item1", "item2", "item3"]');

like image 123
stewe Avatar answered Nov 01 '22 00:11

stewe


Try JSON.parse:

 ary = JSON.parse(s);

Most browsers support it natively, for others you'll need json.js

like image 40
georg Avatar answered Nov 01 '22 02:11

georg