Skip to content

Instantly share code, notes, and snippets.

@jeffpamer
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffpamer/3eae9d46e46d472f9a2a to your computer and use it in GitHub Desktop.
Save jeffpamer/3eae9d46e46d472f9a2a to your computer and use it in GitHub Desktop.
Destructure Restructure Pick
// given this object:
var origObj = {
first_name: 'test',
last_name: 'user',
email_address: 'user@test.com',
otherThing: 1234,
oneOtherThing: 5678
};
// what's the best/coolest way to get:
var user = {
first_name: 'test',
last_name: 'user',
email: 'user@test.com'
};
// 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