Merging arrays that hold CSV data into one object array

Viewed 195

I've taken influence from this post to store CSV files in individual arrays, and it works. I'm looking to add about 8 more arrays that each hold the contents of a CSV file.

But how can I merge these arrays into one array of objects? Having an array of objects is critical for my subsequent application of these arrays. What I have so far is below, but I'm not sure if it is correct or not?

Any help would be appreciated.

var dataCSV;
var dataCSV2;

var collection = {dataCSV, dataCSV2};

d3.csv("data/csv1.csv", function(data) {
    data.forEach(function(d) {
        d.word = d.word;
        d.frequency = +d.frequency;
})
    dataCSV = data;
    console.log(dataCSV);
});

d3.csv("data/csv2.csv", function(data) {
    data.forEach(function(d) {
        d.word = d.word;
        d.frequency = +d.frequency;
    })
    dataCSV2 = data;
    console.log(dataCSV2);
});

function merge(data1, data2) {
    collection = {data1, data2};
    console.log(collection);
}

merge(dataCSV, dataCSV2);

Ideally, I'd like the output of collection to be:

collection {
    dataCSV {
        word: ..., frequency: ...,
        word: ..., frequency: ...
    },
    dataCSV2 {
        word: ..., frequency: ...,
        word: ..., frequency: ...
    }
 }
1 Answers

Instead of repeating yourself, create a single method that gets the data, formats it, and returns a promise. Then collect the results of several promises using Promise.all(). Then you can collect both arrays in an object.

Example (not tested):

const getCsv = (url) => new Promise((resolve) => {
  d3.csv(url, function(data) {
    data.forEach(function(d) { // you can use a Array.map() instead for a cleaner solution
      d.word = d.word;
      d.frequency = +d.frequency;
    })

    resolve(data);
  });
});

Promise.all([ // load both csvs, and wait for the results of both
  getCsv('data/csv1.csv'),
  getCsv('data/csv1.csv')
])
.then(([dataCSV, dataCSV]) => { 
  const collection = { dataCSV, dataCSV2 };
  console.log(collection );
});
Related