How To Convert A Comma Separated String To Array In JavaScript

convert a comma separated string to array in JavaScript

Converting a comma separated string to array is one of the most essential skills that JavaScript developers should know. If you are still confused about how to do it, then don’t worry because in this tutorial, we will give you some common methods to convert a comma separated string to array in JavaScript in just a few steps. 

Convert a comma separated string to array in JavaScript

Method 1: Use string.split()

Problem 1:

Let’s say we have a comma separated string like this: 

var myString = '23,329,443,56,98,347,43,67';

And our job is to convert it to an array.

To deal with this, JavaScript has supported us with the split() method. The split() method will divide the string into substrings using a pattern and then put all of the substrings into an array. 

Syntax: 

string.split(separator, limit)

Parameters: 

  • separator (optional): A string/regular expression that shows where to split.
  • limit (optional): limit the number of splits.

Return value: an array that contains all of the substrings. 

Example 1: 

var myString = 'Welcome to LearnShareIT.com';

result_1 = myString.split('');
result_2 = myString.split(' ');
result_3 = myString.split();

console.log(result_1);
console.log(result_2);
console.log(result_3);

Output:

[ 'W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'L', 'e', 'a', 'r', 'n', 'S', 'h', 'a', 'r', 'e', 'I', 'T', '.', 'c',  'o', 'm' ]
[ 'Welcome', 'to', 'LearnShareIT.com' ]
[ 'Welcome to LearnShareIT.com' ]

In example 1, if we use an empty string (”) as the separator, the split() method will divide each character of the string into substrings. If we use space (‘ ‘) as the separator, it will find each word or string that is separated by the space and divide it into substrings. If we have no separator, the array will contain the whole original strings.

Let’s apply that to solve problem 1. If we want to convert a comma separated string to an array, we will use the comma (‘,’) as the separator: 

var myString = '23,329,443,56,98,347,43,67';
result = myString.split(',');
console.log(result);

Output:

[ '23', '329', '443', '56', '98', '347', '43', '67' ]

Method 2: Get an empty array from an empty string 

When you use the split() method on an empty string, it will return the array that contains the empty string, not an empty array. 

var myStr = '';
result = myStr.split();
console.log(result);

Output:

[ '' ]

If you want to get an empty array instead, you can use the array.filter() method. 

Syntax:

array.filter(function(currentValue, index, arr), thisValue)

Parameters

  • function(): a function or condition that you want to run through every element.
  • currentValue: the current element’s value.
  • index: the current element’s index. 
  • arr: the current element’s array.
  • thisValue: A value passed to the function as its this value.

Completed code: 

var myStr = '';

function check(element) { 
  return element !== '';
}

result = myStr.split().filter(check);
console.log(result);

Output: 

[]

Summary

We have walked you through how to convert a comma separated string to array in JavaScript. The main idea is to use the string.split() method to separate and divide the string into substrings, then add them to a new array. 

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *