0

In d3.csv("file.csv", function(error, data) instruction

d3.csv is a helper that uses d3.csv.parse internally to parse CSV data loaded from a file into an array of objects, and then it passes this to a callback.

So data is a callback variable that holds an array of objects

In the picture the structure of data data

You can see that the headers of the original CSV have been used as the property names for the data objects.Using d3.csv in this manner requires that your CSV file has a header row.

data is array of objects

data (in orange) is a combination of :
- columns array (in red) holding csv headers that means **property names** 
  for the **data objects**
- an array of 6131 elements (in green) holding the values associated with 
  these propertie

Now that we have finished describing the desired structure:

Imagine I have an array of 6131 elements (the same as described in the picture in green)

var dataArray =[];
 for(var i=0;i<6131;i++){
        ddd[i]={x:..,y:...etc};
        dataArray.push(ddd[i]); 
 }

My question is how to construct in reverse the same identical structure described before and result with the same data like the one got from d3.csv.

var columnsArray=["NOM","PRENOM","SPECIALITE","Full_Address","VILLE","lat","lon"];

Thank you very much for help.

1 Answer 1

1

You can attach a columns property to your dataArray object:

dataArray.columns = columnsArray;
Sign up to request clarification or add additional context in comments.

2 Comments

I put here your first answer : because I tested and worked perfectly by adding var desiredArray = d3.csvParse(csv); like the following
function toCSV(row) { return columnsArray.map(column => row[column]).join(','); } var columnsArray= ["NOM","PRENOM","SPECIALITE","Full_Address","VILLE","lat","lon"]; const header = columnsArray.join(','); const rows = mydataArray.map(toCSV).join('\n'); const csv = header + '\n' + rows; var desiredArray = d3.csvParse(csv); please add it as first option in your answer. Thanks Wex !! Fast and great responses as usual, can't thank you enough for putting up with the newbie questions :). Cheers

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.