JavaScript中数组对象的valueOf方法可以将数组的值输出为逗号分割的字符串,下面的代码演示了如何将数组抓换成逗号和竖线分割的字符串
var fruits = ['apple', 'peaches', 'oranges', 'mangoes']; // http://www.75271.com var str = fruits.valueOf(); //输出结果: apple,peaches,oranges,mangoes
如果希望使用竖线|分割
var fruits = ['apple', 'peaches', 'oranges', 'mangoes']; // http://www.75271.com 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'];