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 e.g. I have this string
var str = "January,February,March,April,May,June,July,August,September,October,November,December";
now want to split this by comma and store in Array object
good solution for that
The split() method is used to split a string into an array of substrings, and returns the new array.
Pass your comma Separated string into this function and it will return array, and if not comma separated string found then will return null.
I know this question has been answered for quite a while, but I thought that my contribution would be beneficial to others researching this topic...
Here is a function that will convert a string to an array, even if there is only one item in the list (no separator character):
Use it like this:
I created this function because
split
throws out an error if there is no separator character in the string (only one item)