How to convert Javascript Arrays to CSV (Comma separated values)

本文介绍了如何使用JavaScript将数组转换为CSV格式,包括使用toString()、valueOf()和join()方法,并提供了实例代码演示。

文章源自:http://viralpatel.net/blogs/convert-javascript-arrays-to-csv/

How to convert Javascript Arrays to CSV (Comma separated values)

Let’s see a small code snippet in Javascript that converts JS arrays in Comma separated values. It is also possible to convert an array to delimited text where one can choose the separator.

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];

var str = fruits.toString();

//print str: apple, peaches, oranges, mangoes

 

The toString() method converts an array into a String and returns the result. The returned string will separate the elements in the array with commas.

That’s simple. Javascript Array also has method valueOf() that does the same thing.

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];

var str = fruits.valueOf();

//print str: apple,peaches,oranges,mangoes

 

The valueOf() method returns the primitive value of an array. Again the returned string will separate the elements in the array with commas.

There is no difference in toString() and valueOf(). Try with different data types like number, strings etc it would give the same result.

Also sometimes one wants to create delimited string from an array. Lets say pipe (|) separated string!. In such cases Arrays join() method comes quite handy. The join() method joins the elements of an array into a string, and returns the string.

By default the join() method returns a comma (,) separated value of the array. But you can give argument to join() method and specify the separator. For example:

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];

var str = fruits.join("|");

//print str: apple|peaches|oranges|mangoes

 

How to convert CSV to Array in Javascript

Let us see how to convert a string with commas in it into an Array. Consider following case:

var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];

var str = "apple, peaches, oranges, mangoes";

var fruitsArray = str.split(",");

//print fruitsArray[0]: apple

 Thus split() method of String comes handy for this. It is used to split a string into an array of substrings, and returns the new array. The split() method does not change the original string. Check the sytax of split() method.

string.split(separator,limit)

 

  • separator (Optional). Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item
  • limit (Optional). An integer that specifies the number of splits, items after the split limit will not be included in the array

Note: If an empty string (“”) is used as the separator, the string is split between each character. Thus:

var str="How are you doing today?";
var n=str.split("");

 The result of n will be an array with the values:

H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值