How to Convert String Separated by Commas to Array? [Duplicate]

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

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);
3

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.

make it an array

var array = myString.split(',');
Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest