This article describes the method of converting arrays into CSV format in JavaScript. Share it for your reference. The specific analysis is as follows:
The valueOf method of array object in JavaScript can output the value of an array as a comma-segmented string. The following code demonstrates how to change an array to a comma-segmented string.
var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];var str = fruits.valueOf(); //Output result: apple,peaches,oranges,mangoes
If you want to use vertical lines|split
var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];var str = fruits.join("|"); //print str: apple|peaches|oranges|mangoesThe complete demo code is as follows
Click here to convert fruits array to CSV: <button onclick="javsascript:convert()">Convert to CSV</button><br><pre>var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];</pre><script> function convert() { var fruits = ['apple', 'peaches', 'oranges', 'mangoes']; var str = fruits.valueOf(); alert(str);}</script>I hope this article will be helpful to everyone's JavaScript programming.