Skip to content

Instantly share code, notes, and snippets.

@dtsn
Created October 22, 2013 10:49
Show Gist options
  • Save dtsn/7098527 to your computer and use it in GitHub Desktop.
Save dtsn/7098527 to your computer and use it in GitHub Desktop.
Omit keys from an object
/**
* Omit keys from an object by an Array
*
* @param Object Obj The original object
* @param [[string, ....] The array of blacklisted keys
*
* @returns a clone of the obj with the keys removed
*/
var deepOmit = function (obj) {
var keys = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1));
var recurse = function (keyChain, obj) {
var copy = {};
keyChain = keyChain || [];
for (var key in obj) {
if (keys.indexOf(keyChain.concat(key).join('.')) == -1) {
copy[key] = obj[key];
if (typeof obj[key] === 'object') {
copy[key] = recurse(keyChain.concat(key), obj[key]);
}
}
}
return copy;
};
return recurse([], obj);
};
/*
console.log(deepOmit({
a: 'b',
c: {
d: 'e'
}
}, ['c.d']));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment