Jul

18


Using JavaScript, it’s not only possible but also very easy to convert csv - comma seprated values into array. Using JavaSctipt’s build-in string function ’split’, it can be easily done. Below is an example of retrieve comma separated value from a div block, convert it into an array and redisplay it by writing an array loop.


<html>
<head>
</head>
<body>
<div id="arrayDiv">Windows, Mac, Linux</div><br />
The array is:<br />
<script>
var myTxt = document.getElementById('arrayDiv').innerHTML;
var myArr = myTxt.split(',');
for (var i=0; i<myArr.length; i+=1) {
	document.writeln(myArr[i]+'<br />');
}
</script>
</body>
</html>


Similar Posts

Comments

Name (required)

Email (required)

Website

Speak your mind

2 Comments so far

  1. zeriano on July 20, 2008 10:14 am

    thanks for this great article. i was trying so hard to write a regular expression code to convert csv into array, never know it can actually be done in such an easy way. thanks!

  2. Stephan on August 6, 2008 12:33 pm

    Hi there,

    The CSV format is a bit more tricky, at least as far as Excel is concerned.

    For example, commas can appear within the fields.
    And the fields may be all quoted. Whereupon quotes within the fields are also escaped.

    Stephan

Sponsors




Links