1

I'm using d3 to import a CSV file for data visulisation. My CSV file has header rows that I need to convert to dates, and data that needs to be converted to integers:

ISBN,2019-08,2019-09,2019-10
9782749205465,107,488,218
9789423069313,95,87,186

I can import the CSV file and parse the data as integers using:

d3.csv("/assets/2019-20-download-stats.csv").then(function (data) {
  data.forEach(function(d) {
    d.ISBN = +d.ISBN;
    d['201908'] = +d['201908'];
    d['201909'] = +d['201909'];
    d['201910'] = +d['201910'];
  });
});

but the header rows are all strings in the output array, e.g.:

{
  "201908": 107,
  "201909": 488,
  "201910": 218,
  "ISBN": 9782749205465,
}

How do I format the header rows when importing the data?

1 Answer 1

1

This is almost certainly a XY problem. I can't see any situation where your data should have objects with dates as keys. On top of that, pay attention to the fact that JavaScript will automatically convert your date objects to strings if you pass them as an object's key. If you (for whatever reason) really want a date as key, you should use a Map.

All that being said, here is what you should do: in the row conversion function (you have none in your code), check for a date...

if (!isNaN(new Date(key).getTime()))

... and, if you find one, use new Date to generate the key:

foo[new Date(key)] = +d[key]

Here is a demo using d3.csvParse (which would be exactly the same for your d3.csv):

const csv = `ISBN,2019-08,2019-09,2019-10
9782749205465,107,488,218
9789423069313,95,87,186`;

const data = d3.csvParse(csv, row);

function row(d) {
  const newObject = {};
  for (var key in d) {
    if (!isNaN(new Date(key).getTime())) {
      newObject[new Date(key)] = +d[key]
    } else {
      newObject[key] = +d[key]
    }
  };
  return newObject;
};

console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

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

Comments

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.