JavaScript converts an array to CSV format _javascript tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.