JavaScript Code


How to convert an array into CSV string in JavaScript?


Convert array to CSV string

Given an array of items, write a JavaScript program that converts the given array of items into a CSV string, where the values in the CSV string are the items in the array.

Solution

To convert given array of items into a CSV string using JavaScript, call join() method on the array object and pass comma character (separator) as argument to join() method.

array1.join(',') returns a string with the items of the given array array1 joined by comma.

Programs

1. Get item from array array1 at position index.

let array1 = ['apple', 'banana', 'cherry'];
let csvString = array1.join(',');
console.log(csvString);



copyright @2022