Skip to content

Instantly share code, notes, and snippets.

@wichopy
Last active January 9, 2018 21:08
Show Gist options
  • Save wichopy/213e2acf4e336df76a3edf022c85801f to your computer and use it in GitHub Desktop.
Save wichopy/213e2acf4e336df76a3edf022c85801f to your computer and use it in GitHub Desktop.
Immutable removal of keys from a javascript object using ES6 deconstructing.
//Avoid annoying immutability bugs by using this nice ES6 deconstructing trick to keep your object immutable.
// example obj
const someObj = {
want: 'this',
need: 'that',
dont: 'wantthis',
forget: 'this'
};
const isolateKeys = (object) => {
// deconstruct the keys you dont want, everything you want will be in the the rest variable.
const { dont, forget, ...rest } = object
return rest
}
const keysThatIWant = isolateKeys(someObj)
// Returns:
// {
// want: 'this',
// need: 'that'
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment