Last active
August 29, 2015 14:21
-
-
Save jeffpamer/3eae9d46e46d472f9a2a to your computer and use it in GitHub Desktop.
Destructure Restructure Pick
This file contains hidden or 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
| // given this object: | |
| var origObj = { | |
| first_name: 'test', | |
| last_name: 'user', | |
| email_address: '[email protected]', | |
| otherThing: 1234, | |
| oneOtherThing: 5678 | |
| }; | |
| // what's the best/coolest way to get: | |
| var user = { | |
| first_name: 'test', | |
| last_name: 'user', | |
| email: '[email protected]' | |
| }; | |
| // Ramda pick can get an object with the specific props | |
| var user = R.pick(['first_name', 'last_name', 'email_address'], origObj); | |
| // Object destructuring can rename the email_address prop to 'email' | |
| var { first_name, last_name, email: email_address } = origObj; | |
| // How do I get both? Is the best/simplest way just boring ol': | |
| var user = { | |
| first_name: origObj.first_name, | |
| last_name: origObj.last_name, | |
| email: origObj.email_address | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment