This example describes the way JavaScript converts an array to CSV format. Share to everyone for your reference. The specific analysis is as follows:
The valueof method of an array object in JavaScript can output the value of an array as a comma-delimited string, and the following code shows how to replace an array with a comma-and vertical-bar-delimited string
var fruits = [' apple ', ' Peaches ', ' oranges ', ' mangoes '];
var str = fruits.valueof ();
Output Result: apple,peaches,oranges,mangoes
If you want to use a vertical bar | split
var fruits = [' apple ', ' Peaches ', ' oranges ', ' mangoes '];
var str = fruits.join ("|");
Print Str:apple|peaches|oranges|mangoes
The complete demo code is as follows
Click here to convert fruits array to CSV:
<button onclick= "Javsacript: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 help you with your JavaScript programming.