2

How can I take the following chunk of csv data and convert it to tr's and td's, using javascript?

Jess,Female,04/26/1990,North Central College,Aix,Spring 2012,WebApp,
MC,Female,04/27/1991,Carnegie Mellon University,Aix,Spring 2012,WebApp,
Sharon,Female,04/03/1967,Hobart and William Smith Colleges,Aix,Spring 2012,WebApp,
Nancy,Female,08/15/1989,The New School,Aix,Spring 2011,WebApp,
Jacqueline,Female,03/18/1991,University of South Carolina,Aix,Spring 2011,WebApp,
Sydney,Female,12/11/1990,University of Vermont,Aix,Spring 2011,WebApp,
Kelsey,Female,12/08/1989,University of Vermont,Aix,Spring 2011,WebApp,
Oktavia,Female,11/05/1990,SUNY - Albany,Aix,Spring 2011,WebApp,
Courtney,Female,12/02/1988,Ithaca College,Aix,Spring 2009,WebApp,
Nike,Female,24.2.1989,Appleby College,Aix,Spring 2008,WebApp,
Linda,Female,8/26/1964,Kalamazoo College,Aix,Spring 2009,WebApp,
Allison,Female,12/15/1976,University of San Diego,Aix,Spring 2009,WebApp,
Carmen,Female,02/07/1988,Carnegie Mellon University,Aix,Spring 2008,WebApp,
Nora,Female,10/23/88,Eastern Washington University,Aix,Spring 2009,WebApp,
Jennifer,Female,10/27/79,University of Kansas,Aix,Spring 2009,WebApp,

Desired Table Format for each of the lines in the csv data.

<tr><td>Jess</td> <td>Female<td><td>04/26/1990</td><td>North Central College</td><td>Aix</td><td>Spring 2012</td><td>WebApp</td></tr>
3
  • In JavaScript? Is that CSV retrieved via Ajax, or...? Commented Jan 5, 2013 at 1:46
  • Sorry, Yes JavaScript and the csv data is retrieved via Ajax. Just updated the question. Commented Jan 5, 2013 at 1:46
  • @CarlWeis Yes, but how are you getting the data in to Javascript? Commented Jan 5, 2013 at 1:47

2 Answers 2

11

Assuming you have that CSV data in a variable (whether retrieved via Ajax or whatever) then you can use the .split() method to get an array of lines and split each line on commas:

var data = // your data
var lines = data.split("\n"),
    output = [],
    i;
for (i = 0; i < lines.length; i++)
    output.push("<tr><td>"
                + lines[i].slice(0,-1).split(",").join("</td><td>")
                + "</td></tr>");
output = "<table>" + output.join("") + "</table>";

(The string .slice() is to ignore the trailing commas on each line.)

Demo: http://jsfiddle.net/frvQ2/

Sign up to request clarification or add additional context in comments.

10 Comments

How would I do the reverse. Take the html just the tr's and td's and convert those to the csv format? Because it's not working with some of the data I have.
yourHTML.replace(/<tr>|<td>/g,"").replace(​​​​​​​​​​​​​​​​​​/<\/td>/g,",").replace(/<\/tr>/g,"\n") would turn a string of tr and td elements back into CSV. Or adapt the code in my answer to split on the tags and join on commas and newline characters...
keep getting object has no method push
@josh - .push() is an array method. Is your object an array as shown in my answer?
@josh - I'm not sure what you mean by "cache it" - the i variable the way I did it has exactly the same scope as the way you showed in your comment (JS doesn't have block scope for variables declared with var, only function scope, so your change would have no effect on execution).
|
3

How about:

var data = //your data

data = "<table><tr>" + 
  data.replace(/,\n/g,"<tr>")
      .replace(/,/g, "<td>")
      .replace(/<tr>$/,"") +
  "</table>";

2 Comments

This does not include proper end tags. For example </tr>
The end tags are optional for TR and TD. w3.org/TR/html401/struct/tables.html#h-11.2.5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.