Alright, guys
I'm new to promises and am trying to map the result of loading in CSVs via promises, to then perform subsequent applications on it. Is Async mapping allowed? Is there an alternative approach to map after using promises? Or am I being stupid and missing something completely simple?
var csv = (url) => new Promise(function(resolve) {
d3.csv(url, function(data) {
data.forEach(function(d) {
d.String = d.String;
d.Integer = +d.Integer;
})
resolve(data);
});
});
Promise.all([
csv('content.csv'),
csv('moreContent.csv')
])
.then(([csvOne, csvTwo]) => {
var collection = { csvOne, csvTwo };
var aveageMap = {};
collection.map((d,i) => {
if (!aveageMap[d.String]) {
aveageMap[d.String] = {
sum: 0,
average: 0
};
}
var theWord = aveMap[d.String];
theWord.sum += +d.Integer;
theWord.average = theWord.sum / collection.length;
});
console.log(aveageMap);
});
For the comments and answers below
Using an array is an object, but returns a complete merger of the two CSVs... Which affects the theWord.sum / collection.length line... Whilst it works to some degree, I wanted to use an array of objects so that I could get an output like the one below, and access each object independently if needed.
collection {
dataCSV {
String: , Integer: ,
String: , Integer: ,
},
dataCSV2 {
String: , Integer: ,
String: , Integer: ,
}
}