473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

CSV to Javascript arrays

Hi,

I have a requirement to convert CSV values entered by user into a Javascript array (of arrays).

No server roundtrip should be made and it should be totally offline/client-side.

If possible, the script should be browser independent.

I have seen a previous post http://www.thescripts.com/forum/thread146456.html but could not find any code level hints.

I know I could use split function and write my own parsing code. But if it is a single line I know the logic can be something like the below, but what if there is a multi-line CSV data in a TextArea?

Expand|Select|Wrap|Line Numbers
  1. function jsSplitString(stringToSplit){
  2.  
  3. var splitArray = stringToSplit.split(",");
  4.  
  5. for (var i = 0; i < splitArray.length; i++){
  6. // add to array 
  7. }
  8.  
  9. }
  10.  
-Ramesh.
Oct 22 '07 #1
3 11801
gits
5,390 Expert Mod 4TB
hi ...

welcome to TSDN ...

in case you have a multiline text you may use something like the following example, t is a multiline text:

Expand|Select|Wrap|Line Numbers
  1. // multiline text
  2. var t = 'abcx,hjhk,hjsahk,klö\nabcx,hjhk,hjsahk,klö\n';
  3.  
  4. // we alert it to have a look :)
  5. alert(t);
  6.  
  7. // we split it first at the linebreaks
  8. var ts = t.split(/\n|\n\r/);
  9.  
  10. // alert first line
  11. alert(ts[0]);
  12.  
now you may combine this with your code ... and you should be on track :)

kind regards
Oct 22 '07 #2
mrhoo
428 256MB
Expand|Select|Wrap|Line Numbers
  1. function csvArray(csv){
  2.     var i= 0, A= csv.split(/\s*\n\s*/);
  3.     while(A[i++]) A[i]= A[i].split(/ *, */);
  4.     return A;
  5. }
Every item in A will be an array of that line's data.
It will work for one line or any number of lines-
you don't often need a one line csv file.
In html terms, each A[index] array could be a row of cells.
Oct 22 '07 #3
gits
5,390 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. function csvArray(csv){
  2.     var i= 0, A= csv.split(/\s*\n\s*/);
  3.     while(A[i++]) A[i]= A[i].split(/ *, */);
  4.     return A;
  5. }
Every item in A will be an array of that line's data.
It will work for one line or any number of lines-
you don't often need a one line csv file.
In html terms, each A[index] array could be a row of cells.
note: this will trim all leading and ending spaces of a line due to the use of \s* ...

kind regards
Oct 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: annoyingmouse2002 | last post by:
Hi there, sorry if this a long post but I'm really just starting out. I've been using MSXML to parse an OWL but would like to use a different solution. Basically it reads the OWL (Based on XML)...
13
by: Kevin | last post by:
Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
1
by: Alfredo Magallón Arbizu | last post by:
Hello, I need to pass the contents of a dataset to the client in order to use these contents with javascript. What is the best way to achieve that? Normally I use controls like a grid or...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.