10 examples of 'javascript read csv' in JavaScript

Every line of 'javascript read csv' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
95function arrayToCSV(rows) {
96 var content = "";
97 rows.forEach(function(row, index) {
98 content += row.join(",") + "\n";
99 });
100 return content;
101}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

39function getCSV() {
40 var XMLHttp = new XMLHttpRequest();
41 XMLHttp.open("GET", "comment_parser.php?format=CSV", true);
42 XMLHttp.onload = function() {
43 // Now we have the XML data, extract
44 var file = [XMLHttp.response];
45 var bb = new Blob(file, {
46 type: 'text/csv'
47 });
48 generateLink(bb, ".csv");
49 }
50 XMLHttp.send();
51}
15function onCsv(f: (csv: [number, number][]) => void) : void {
16 csv('ny-vs-sf.csv')
17 .row((d: {NY: string, SF: string}) => [
18 parseFloat(d.NY.split(';')[0]),
19 parseFloat(d.SF.split(';')[0]),
20 ])
21 .get((error: null, data: [number, number][]) => {
22 if (error != null) {
23 alert('Data can\'t be downloaded or parsed')
24 return
25 }
26 f(data)
27 })
28}
4export async function parseCSV(csv: string): Promise<string[][]> {
5 return new Promise<string[][]>((resolve, reject) => {
6 parse(
7 csv,
8 { relax_column_count: true, skip_empty_lines: true, trim: true },
9 (err, rows) => {
10 if (err) reject(err)
11 else resolve(rows)
12 }
13 )
14 })
15}
61export function convertCSVToJSON(csv, keySeparator = ',', lineSeparator = '\r\n', indent = 2) {
62 if (!csv) {
63 return '';
64 }
65 /** @type {?} */
66 const csvArray = csv.split(lineSeparator);
67 // Input CSV must have a minimum of two rows
68 if (csvArray.length < 2) {
69 return '';
70 }
71 /** @type {?} */
72 const newObjects = [];
73 // Extract object keys from header row
74 /** @type {?} */
75 const keys = csvArray[0].split(keySeparator);
76 // Iterate through array, creating one output line per object
77 for (let i = 1; i < csvArray.length; i++) {
78 /** @type {?} */
79 const newObject = {};
80 /** @type {?} */
81 const values = csvArray[i].split(keySeparator);
82 if (values.length !== keys.length) {
83 continue;
84 }
85 for (let j = 0; j < keys.length; j++) {
86 newObject[keys[j]] = values[j];
87 }
88 newObjects.push(newObject);
89 }
90 return formatJSON(newObjects, indent);
91}
27function csv(options, callback){
28 var path = options.path;
29 fs.readFile(path, 'utf8', function(err,d){
30 data = d;
31 data = data.toString().replace(/\r/g,"").split("\n");
32 var header = data[0].split(",");
33 data = data.slice(1).map(function(d){
34 var line = {};
35 d.split(",").forEach(function(d,i){
36 line[header[i]] = d;
37 });
38 return line;
39 });
40 exports.data = data;
41 callback();
42 });
43}
35export default function readCsv (path, opts_, cb) {
36 var parserOptions
37 if (typeof cb === 'undefined') {
38 cb = opts_
39 } else {
40 parserOptions = typeof opts_ === 'function' ? {map: opts_} : opts_
41 }
42 readData(path, {parser: parserCsv, parserOptions: parserOptions}, cb)
43}
20function processCSVData(csvdata) {
21 var table = [];
22 var lines = csvdata.split(/\r\n|\n/);
23
24 for ( var i = 0; i < lines.length; i++) {
25 var row = lines[i].split(',');
26 table.push(row);
27 }
28 return table;
29};
17function getDataPointsFromCSV(csv, field) {
18 var dataPoints = csvLines = points = [];
19 csvLines = csv.split(/[\r?\n|\r|\n]+/);
20
21 for (var i = (csvLines.length - 8); i < csvLines.length; i++)
22 if (csvLines[i].length > 0) {
23 points = csvLines[i].split(",");
24 dataPoints.push({
25 x: new Date(points[0]),
26 y: parseFloat(points[field]),
27 });
28 }
29 return dataPoints;
30}
15function parseCSV(err, csvData) {
16 if (err) console.log(err);
17 csvParser(csvData, { delimiter: ',' }, readCSV);
18}

Related snippets