JavaScript Code


How to convert CSV string to array in JavaScript?


Convert CSV string to array in JavaScript

In this tutorial, you are given a CSV string. Convert the CSV string into an array of values using JavaScript.

Solution

To convert the given CSV string into an array of values using JavaScript, use String split() method and pass comma character as delimiter/separator string.

str.split(',')

Programs

1. Split the CSV string value in str by comma.

let str = 'apple,banana,cherry';
let valuesArray = str.split(',');
console.log(valuesArray[0]);
console.log(valuesArray[1]);
console.log(valuesArray[2]);