Welcome 微信登录

首页 / 脚本样式 / JavaScript / JavaScript将数组转换成CSV格式的方法

本文实例讲述了JavaScript将数组转换成CSV格式的方法。分享给大家供大家参考。具体分析如下:
JavaScript中数组对象的valueOf方法可以将数组的值输出为逗号分割的字符串,下面的代码演示了如何将数组抓换成逗号和竖线分割的字符串
var fruits = ["apple", "peaches", "oranges", "mangoes"];var str = fruits.valueOf();//输出结果: apple,peaches,oranges,mangoes
如果希望使用竖线|分割

var fruits = ["apple", "peaches", "oranges", "mangoes"];var str = fruits.join("|");//print str: apple|peaches|oranges|mangoes
完整演示代码如下
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>
希望本文所述对大家的javascript程序设计有所帮助。