Home >Web Front-end >JS Tutorial >JavaScript method to convert array into CSV format_javascript skills

JavaScript method to convert array into CSV format_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:08:291974browse

The example in this article describes how to convert an array into CSV format using JavaScript. Share it with everyone for your reference. The specific analysis is as follows:

The valueOf method of the array object in JavaScript can output the value of the array as a comma-separated string. The following code demonstrates how to convert the array into a comma- and vertical-bar-separated string

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];
var str = fruits.valueOf();
  
//输出结果: 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|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 class="brush:php;toolbar:false">var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];
<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 design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn