Reputation: 255
I need to import 8-10 CSV files into some JS code. Each file has about 8 different columns. I would like to store this information in an object (possibly identifying the object based on one of the ID fields). The problem is, the easiest way I can think of looks like this:
var authorArr = [];
d3.csv("data/authors.csv", function(csv) {
csv.forEach(function(d) {
authorArr[d.record_id] = [];
d.record_id = +d.record_id;
d.name = d.name
d.name_inverted = d.name_inverted;
d.role = d.role;
d.sequence = +d.sequence;
d.is_person = d.is_person;
authorArr[d.record_id] = d.name;
authorArr[d.record_id] = d.role;
etc...
Surely this cannot be the quickest way...Also, there are quite a bit of duplicated record_id values as well, so every time there is a repeat, I would lose all previous data with my horrible approach.
I'm not sure if that's enough detail. If not, I would be glad to add more.
Upvotes: 1
Views: 3219
Reputation: 10824
You can use a temporary variable for the new/current record. Your example also looks like you do not want an array (with gaps) but a map. Here is how I would load the data:
var authorMap = {}
var loadCSV = function(file){
d3.csv(file, function(error, data) {
data.forEach(function(d) {
var a = authorMap[d.record_id] = {}
a.record_id = d.record_id
a.name = d.name
a.role = d.role
});
});
}
loadCSV("file1.csv")
loadCSV("file2.csv")
Upvotes: 1