I recently learned this trick which I am sure is not new, but I sort of discovered on my own
The issue is you want your mapping function to be customizable, but the standard javascript mapping function only takes one arg, the value out of the thing being mapped
so the trick is to generate your mapping function with all your arguments in the closure, meaning you write a function that returns the mapping function. your function will take all your args
// here 'band' is the arg that we want to use to generate a custom mapping function
var mapFuncGenerator = function(band) {
return function(rawdata) {
var ts = rawdata.ts;
var compositeValue = rawdata[band][4];
return [ts, compositeValue];
}
};
// use three different mapping functions based on value of 'band'
var graphAlpha = doc.data.map(funcMap('alpha'));
var graphBeta = doc.data.map(funcMap('beta'));
var graphGamma = doc.data.map(funcMap('gamma'));
Cool!
Comments
Post a Comment