Reputation: 3587
I am trying to parse a csv:
col1,col2,col3
a,b,1
d,e,2
when I run the following code
var fs = require("fs");
var d3 = require("d3");
fs.readFile("test.csv", "utf8", function(error, data) {
var data = JSON.stringify(d3.csvParse(data));
data.map((item, i) => console.log(`Hello ${item.col1} this ${item.col2} there are ${item.col3} blabla`));
});
I get TypeError: data.map is not a function
While the following is running fine.
var data = [{ "col1":"a","col2":"b","col3":1},
{ "col1":"d","col2":"e","col3":2}]
data.map((item, i) => console.log(`Hello ${item.col1} this ${item.col2} there are ${item.col3} blabla`));
Upvotes: 2
Views: 3770
Reputation: 370699
d3.csvParse
returns an array, but you convert it to a string here:
var data = JSON.stringify(d3.csvParse(data));
Strings don't have .map
, of course. You probably want to iterate over the array:
const newData = d3.csvParse(data);
newData.forEach((item, i) => console.log(`Hello ${item.col1} this ${item.col2} there are ${item.col3} blabla`));
(don't use .map
because you aren't transforming it into a new array, at least not in the code you've posted)
Upvotes: 1