Created
April 30, 2014 19:38
-
-
Save ryanpitts/5ef5f5b58f3a4e909dc2 to your computer and use it in GitHub Desktop.
processing data based on values in an object, which must be filtered based on key contents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the issue here: We want to ignore census "label columns" for certain charts. | |
// We can identify these columns by the ".5" in their columnID. We're working | |
// from a `comparison.table.columns` object, which has columnIDs as keys. So | |
// the job here is to ignore all the columns in this object that have a "." | |
// in their key, and process the rest into chart data. | |
// one way is to grab a set of keys without ".", then make an object out of | |
// the columns that match the "good" keys, and process those for the chart | |
var filteredKeys = _.filter(_.keys(comparison.table.columns), function(k) { return k.indexOf('.') == -1 }), | |
filteredColumns = _.pick(comparison.table.columns, filteredKeys) | |
_.each(filteredColumns, function(v, k) { | |
// do the processing | |
}); | |
// another way is to just send all the columns through the loop, | |
// and only process the ones that don't have a "." in their key. | |
// this feels simpler, so why does it feel more hacky? | |
_.each(comparison.table.columns, function(v, k) { | |
if (k.indexOf('.') == -1) { | |
// do the processing | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i have no problem with the latter (at line 22). If it feels hacky, I think it's because of the Bureau's casual approach to metadata as much as anything else. Maybe it also feels a little hacky because you have to depend on Underscore for the functional bits as opposed to having them as language built-ins (so you end up with more verbose code).
The slightly less hacky thing, if comparison.table or comparison.table.columns is a object beyond a mere array, would be to implement it as an object method, jus to encapsulate the behavior.