Created
February 9, 2018 21:08
-
-
Save brooksbecton/697dd3115ac4b42d3aeedbcb36485c25 to your computer and use it in GitHub Desktop.
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
/** | |
* 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