How to use the split() function In Javascript
Converting CSVs into Arrays: Using the split() function Now let us see how we can convert a string with comma-separated values into an Array.
javascript
let csv = "geeks, 4, geeks" let array = csv.split( ", " ); console.log(array[0]); console.log(array[1]); console.log(array[2]); |
geeks 4 geeks
Thus split() method of String comes in handy for this. It is used to split a string into an array of substrings and returns the new array. The split() method does not change the original string.
Converting JavaScript Arrays into CSVs and Vice-Versa
Converting Arrays into CSVs: Given an array in JavaScript and the task is to obtain the CSVs or the Comma Separated Values from it. Now, JavaScript being a versatile language provides multiple ways to achieve the task.
Some of them are listed below.
- Using the toString() method
- Using valueof() Method
- Using the join() function
- Using the split() function