Last active
January 9, 2018 21:08
-
-
Save wichopy/213e2acf4e336df76a3edf022c85801f to your computer and use it in GitHub Desktop.
Immutable removal of keys from a javascript object using ES6 deconstructing.
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
//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