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
Pass your comma-separated string into this function and it will return an array, and if a comma-separated string is not found then it will return null.
function splitTheString(CommaSepStr) {
var ResultArray = null;
// Check if the string is null or so.
if (CommaSepStr!= null) {
var SplitChars = ',';
// Check if the string has comma of not will go to else
if (CommaSepStr.indexOf(SplitChars) >= 0) {
ResultArray = CommaSepStr.split(SplitChars);
}
else {
// The string has only one value, and we can also check
// the length of the string or time and cross-check too.
ResultArray = [CommaSepStr];
}
}
return ResultArray;
}