Javatpoint Logo
Javatpoint Logo

How to Convert Comma Separated String into an Array in JavaScript

A comma-separated valuation (CSV) file is a demarcated file format that uses a comma to isolate the values. The data record is comprised of one or more than one field, separated by the commas. The name root for this file format is the comma-separated file format, is the use of the comma as a field extractor.

You can convert the comma-separated string into an array by using the two following methods.

  • By using the split () method
  • Append through the list and put track of each comma you find to generate a new sequence with different strings.

By using the split () method

The split () method is used to partition a sequence based on an extractor. This separator can be delineated as a comma to differentiate the string if a comma is encountered. This procedure returns an array of distinct strings.

Syntax

Example

Here, an illustration is given below for the same.

Output

After the successful execution of the output, we got the following output.

How to Convert Comma Separated String into an Array in JavaScript

Append through the list and put track of each comma you find to generate a new sequence with different strings

This method helps you to iterate the characters of the string and analyze for the comma. The Previous index variable can be determined, which keeps records of the first character of the next string. The slice method is used to remove the part of the string between the prior index and the specific position of the comma found. This string is driven to a new array. This procedure is iterated for the entire length of the string. The final segment comprises all the separate strings.

Syntax

Example

Here, an illustration is given below for the same.

Output

You get the following snippet after the execution of the code.

How to Convert Comma Separated String into an Array in JavaScript




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA