Created
October 22, 2013 10:49
-
-
Save dtsn/7098527 to your computer and use it in GitHub Desktop.
Omit keys from an object
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
/** | |
* 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