// Read CSV data from text area
var data = document.getElementById("csvData").value;
// Trasform it into array of lines trimming any new lines at the end
var lines = data.replace(/\n+$/, "").split("\n"),
output = [],
sum = 0;
// Iterate over each line
lines.forEach(function(line, index) {
console.log(line);
console.log(index);
// very first row of the data would become table header row
if(index === 0) {
output.push("<tr><th>" + line.split(",").join("</th><th>") + "</th></tr>");
} else {
// rest of the lines would be rows of the table
output.push("<tr><td>" + line.split(",").join("</td><td>") + "</td></tr>");
// Sum up 3rd column of the CSV table
sum += parseInt(line.split(",")[2]);
}
});
// Wrap it all in table tags
output = "<table>" + output.join("") + "</table>";
// Append to demo paragraph for this example
document.getElementById("demo").innerHTML = output;
// Append to sum-demo paragraph for this example
document.getElementById("sum-demo").innerHTML = "Your sum is " + sum.toString();
// For magic mirror getDom method you probably would end up just appending it to a wrapper and returning it like next 3 commented lines
// var wrapper = document.createElement("div");
// wrapper.innerHTML = output;
// return wrapper;
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!