d3 / d3-dsv

A parser and formatter for delimiter-separated values, such as CSV and TSV.

Home Page:https://d3js.org/d3-dsv

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dsv.parse's returned columns property doesn't reflect row conversion function

vassudanagunta opened this issue · comments

d3.csv.parse('a,b,c\n1,2,3', function (row) { return {x: row.a, y: +row.b * +row.c} })

returns [ { x: '1', y: 6 }, columns: [ 'a', 'b', 'c' ] ].

Should it return [ { x: '1', y: 6 }, columns: [ 'x', 'y' ] ] ?

I guess the question is: Is the columns property meant to reflect the input or the output?

If the latter, and given there is no guarantee that the row conversion function produces objects with identical properties for every row, perhaps columns could reflect those of the first row?

I think the intention of columns is to reflect the input, not the output.

Makes sense.

I'm trying to figure out how to write generic code that will, in the simplest example, (1) parse a CSV, (2) appy a map function to it, and (3) generate an HTML table using D3. In other words, with step 3 not knowing anything in advance about the CSV or the map applied to it.

I'm new to both Javascript and D3, so it's likely that I simply don't know the smart Javascript way to do this.

How about something like

const outputColumns = Object.keys(data[0]);

Perfect. Thank you.

I'll let you close this issue given that you wrote "I think..." above.