Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string separated by commas to array? [duplicate]

Tags:

javascript

Possible Duplicate:
Convert JS object to JSON string
Store comma separate values into array

I have a string containing values separated with commas:

"1,4,5,11,58,96"

How could I turn it into an object? I need something like this

["1","4","5","11","58","96"]
like image 708
Mindaugas Jakubauskas Avatar asked Jan 12 '13 00:01

Mindaugas Jakubauskas


3 Answers

This will convert it into an array (which is the JSON representation you specified):

var array = myString.split(',');

If you need the string version:

var string = JSON.stringify(array);
like image 78
joeltine Avatar answered Nov 16 '22 10:11

joeltine


In JSON, numbers don't need double quotes, so you could just append [ and ] to either end of the string, resulting in the string "[1,4,5,11,58,96]" and you will have a JSON Array of numbers.

like image 3
tiffon Avatar answered Nov 16 '22 08:11

tiffon


make it an array

var array = myString.split(',');
like image 2
kevin Avatar answered Nov 16 '22 09:11

kevin