Skip to content

Instantly share code, notes, and snippets.

@thymikee
Last active July 12, 2016 11:37
Show Gist options
  • Save thymikee/e4e9dc5c776d98af49bb6cf9105343ec to your computer and use it in GitHub Desktop.
Save thymikee/e4e9dc5c776d98af49bb6cf9105343ec to your computer and use it in GitHub Desktop.
Omits keys from JS object
/**
* 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