Last active
July 12, 2016 11:37
-
-
Save thymikee/e4e9dc5c776d98af49bb6cf9105343ec to your computer and use it in GitHub Desktop.
Omits keys from JS object
This file contains 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
/** | |
* Omits keys from object functional style. | |
* | |
* @param {object} obj – object to omit keys from. | |
* @param {string} ...keys – array of string keys to extract from object. | |
* @return {object} – new `obj` without `keys`. | |
* | |
* Example: | |
* omit({ a: 1, b: 2, c: 3, x: 4, y: 5 }, 'a', 'x') returns { b: 2, c: 3, y: 5 } | |
* | |
*/ | |
const omit = (obj, ...keys) => { | |
if (typeof obj !== 'object') { return obj; } | |
return Object.keys(obj).reduce((newObj, next) => | |
(keys.indexOf(next) === -1 ? Object.assign({}, newObj, { [next]: obj[next] }) : newObj), | |
{}); | |
}; | |
export default omit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment