HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

How can I convert a comma-separated string to an array?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
arrayhowcommaconvertseparatedcanstring

Problem

I have a comma-separated string that I want to convert into an array, so I can loop through it.

Is there anything built-in to do this?

For example, I have this string

var str = "January,February,March,April,May,June,July,August,September,October,November,December";


Now I want to split this by the comma, and then store it in an array.

Solution

const array = str.split(',');


MDN reference, mostly helpful for the possibly unexpected behavior of the limit parameter. (Hint: "a,b,c".split(",", 2) comes out to ["a", "b"], not ["a", "b,c"].)

Code Snippets

const array = str.split(',');

Context

Stack Overflow Q#2858121, score: 1516

Revisions (0)

No revisions yet.