Skip to content

Instantly share code, notes, and snippets.

@brooksbecton
Created February 9, 2018 21:08
Show Gist options
  • Save brooksbecton/697dd3115ac4b42d3aeedbcb36485c25 to your computer and use it in GitHub Desktop.
Save brooksbecton/697dd3115ac4b42d3aeedbcb36485c25 to your computer and use it in GitHub Desktop.
/**
* Takes in an object to be filtered and
* an array of keys to remove
*
* Similar to
* PHP - http://php.net/manual/en/function.array-diff.php
* C# - https://msdn.microsoft.com/en-us/library/bb534803(v=vs.110).aspx
* Ruby - http://api.rubyonrails.org/classes/Hash.html#method-i-except
*
* @param {Object} object
* @param {Array} filter
* @returns {Object} object without key/values of keys in filter
*/
function except(object, filter) {
let newObj = {};
Object.keys(object).forEach(key => {
if (filter.indexOf(key) === -1) {
newObj[key] = object[key]
}
})
return newObj;
}
const hash = { a: true, b: false, c: null }
const newObj = except(hash, ['c']); // { a: true, b: false }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment