array from comma separated string javascript

Array from Comma Separated String in JavaScript

When working with JavaScript, it is often useful to convert a comma separated string into an array. This can be done using a variety of methods, which we will explore below.

Method 1: Using the split() function

The most common method for converting a comma separated string into an array is by using the split() function. This function takes a delimiter (in this case a comma) and splits the string at each occurrence of that delimiter, returning an array of the resulting substrings.


var str = "apple,banana,orange";
var arr = str.split(",");
console.log(arr); // Output: ["apple", "banana", "orange"]

In the example above, we declare a string "apple,banana,orange" and then use the split() function to convert it into an array. The resulting array contains three elements: "apple", "banana", and "orange".

Method 2: Using the split() function with trim()

Another option is to use the split() function in combination with the trim() function to remove any leading or trailing white space from each substring in the resulting array.


var str = "apple, banana ,orange";
var arr = str.split(",").map(function(item) {
  return item.trim();
});
console.log(arr); // Output: ["apple", "banana", "orange"]

In this example, we declare a string "apple, banana ,orange" with extra white space around the second element. We use the split() function to convert it into an array and then use the map() function to apply the trim() function to each element in the resulting array. The resulting array is the same as in the previous example: ["apple", "banana", "orange"].

Method 3: Using the from() function

The from() function is another option for converting a comma separated string into an array. This function creates a new array from an array-like object (in this case, a string), using an optional map function to modify each element in the resulting array.


var str = "apple,banana,orange";
var arr = Array.from(str.split(","));
console.log(arr); // Output: ["apple", "banana", "orange"]

In this example, we first use the split() function to convert the string into an array of substrings. We then pass that array into the from() function to create a new array with the same elements. The resulting array is the same as in the previous examples.

Method 4: Using the regular expression match() function

Finally, it is possible to use regular expressions and the match() function to convert a comma separated string into an array. This method can be useful if you need to split the string based on more complex rules than just a single delimiter.


var str = "apple,banana;orange";
var arr = str.match(/[^,;]+/g);
console.log(arr); // Output: ["apple", "banana", "orange"]

In this example, we declare a string "apple,banana;orange" with two different delimiters: a comma and a semicolon. We use the match() function with a regular expression to split the string based on any character that is not a comma or semicolon. The resulting array contains three elements: "apple", "banana", and "orange".

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe