Jul
18
JavaScript Convert CSV to Array
July 18, 2008 |
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
- JavaScript Stop a Loop
- JavaScript - Ways to Create Markup
- JavaScript - Get Form Checkbox Array Values
- JavaScript Control Flash Replay
- JavaScript - Unobtrusive Slideshow
- Prototype Drop Down Menu
- JavaScript - Remove the Last Character of a String
- Prototype Function Variable inside Observe
Comments
2 Comments so far



































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!
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