Though I can achieve most ends via a series of conditionals and arithmetic, especially when iterating over or comparing arrays I frequently hear of much more efficient implementations.
Hailing from PHP, I am accustomed to having array_merge() accessible; this JS function is different in that key that don't match the first array should be discarded (and generally, I'm using this sort of thing as a way of generating config objects for various parameterized components of a larger project).
I was hoping a serious comp-sci nerd could comment to the efficiency of this implementation of the same in JavaScript, and if it can be improved, explain—even very briefly—what's happening, computationally as it were. I have the intuitive sense that there it a bit-wise operation that could achieve this faster:
function mapConfig(template, cfg) {
var configured_template = {};
for (key in cfg) {
template[key] = key in template ? cfg[key] : false;
}
return template
}
var template = {key1:false, key2:false, key3:false};
var cfg = {key1:true, keyTWO:"puppies", key3:false};
var cfg_obj = mapConfig(template, cfg);
console.log(cfg_obj);
//output >>> {key1:true, key2:false, key3:false}
I appreciate any insights anyone could offer.
forbid_flagsin your function? I don't see it defined anywhere and why not just use anifstatement if there's only one arm to your conditional? \$\endgroup\$configured_templatedo ? \$\endgroup\$