Convert comma separated string to array using JavaScript
Last Updated :
07 Dec, 2023
In this article, we will see how to convert a comma-separated string to an array using JavaScript. A comma-separated string can be converted to an array using the following approaches:
Methods to Convert Comma-separated String to JavaScript Array:
Method 1: Using the JavaScript split() method
The split() method is used to split a string based on a separator. This separator could be defined as a comma to separate the string whenever a comma is encountered. This method returns an array of strings that are separated.
Syntax:
string.split(', ');
Example: In this example, we will be using the split() method to convert a comma-separated string to an array using Javascript.
Javascript
function separateString() {
originalString = "One, Two, Three, Four, Five" ;
separatedArray = originalString.split( ', ' );
console.log(separatedArray);
}
separateString();
|
Output
[ 'One', 'Two', 'Three', 'Four', 'Five' ]
This approach involves iterating through each character in the string and checking for the comma. A variable previousIndex is defined which keeps track of the first character of the next string. The slice method is then used to remove the portion of the string between the previous index and the current location of the comma found. This string is then pushed onto a new array. This process is then repeated for the whole length of the string. The final array contains all the separated strings.
Syntax:
string.slice(startingIndex, endingIndex);
Example: In this example, we will be converting a comma-separated value to a Javascript string.
Javascript
originalString = "One, Two, Three, Four, Five" ;
separatedArray = [];
let previousIndex = 0;
for (i = 0; i < originalString.length; i++) {
if (originalString[i] == "," ) {
separated = originalString.slice(previousIndex, i);
separatedArray.push(separated);
previousIndex = i + 1;
}
}
separatedArray.push(originalString.slice(previousIndex, i));
console.log(separatedArray);
|
Output
[ 'One', ' Two', ' Three', ' Four', ' Five' ]
The Javascript Array.from() method is used to create a new array instance from a given array, string, or object.
Syntax:
Array.from(object, mapFunction, thisValue);
Example: In this example, we will use Array.from() and .spilt() methods to split the strings and convert them to an array.
Javascript
originalString = "One, Two, Three, Four, Five" ;
const separatedArray = Array.from(
originalString.split( "," )
);
console.log(separatedArray);
|
Output
[ 'One', ' Two', ' Three', ' Four', ' Five' ]
In this approach, we are using the Lodash _.split() method that returns the array of the gievn value having definite separator.
Example: In this example, we are using Lodash _.split() method.
Javascript
const _ = require( 'lodash' );
let str = "Geeks,for,Geeks" ;
console.log(_.split(str, '-' , 1))
|
Output:
[ 'Geeks', 'for', 'Geeks' ]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...