Skip to content

Instantly share code, notes, and snippets.

@johannesboyne
Created February 5, 2013 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johannesboyne/4715413 to your computer and use it in GitHub Desktop.
Save johannesboyne/4715413 to your computer and use it in GitHub Desktop.
Dynamic field/attribute hiding from an object. (I named it fieldHider because we used it to hide mongodb results)
// this is used to work in Node.js
// if you want to use it inside the browser, replace the map function to ensure older browser support.
// DEPENDENCIES: underscore!
function omitFields (model, hidden_fields) {
var hider_code = 'return _.omit(model';
hidden_fields.map(function (hider) {
hider_code += ', \''+hider+'\'';
});
hider_code += ');';
return new Function('_', 'model', hider_code)(_, model);
}
// use
omitFields({name: "Mr. Johnson", password: "PWHASH", secret: "mystery"}, ['password', 'secret']);
// => return: {name: "Mr. Johnson"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment