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
Release: 2016-05-16 16:08:29
Original
2009 people have browsed it

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
Copy after login

If you want to use vertical lines | split

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];
var str = fruits.join("|");
  
//print str: apple|peaches|oranges|mangoes
Copy after login

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>
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template