Skip to content

Instantly share code, notes, and snippets.

@joelnet
Created March 1, 2019 07:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joelnet/d8ef09c15d83485cf684e98ca1e55735 to your computer and use it in GitHub Desktop.
Save joelnet/d8ef09c15d83485cf684e98ca1e55735 to your computer and use it in GitHub Desktop.
const user1 = {
id: 100,
name: 'Howard Moon',
password: 'Password!'
}
const removeProperty = prop => ({ [prop]: _, ...rest }) => rest
// ---- ------
// \ /
// dynamic destructuring
const removePassword = removeProperty('password')
const removeId = removeProperty('id')
removePassword(user1) //=> { id: 100, name: 'Howard Moon' }
removeId(user1) //=> { name: 'Howard Moon', password: 'Password!' }
@viksok
Copy link

viksok commented Mar 20, 2019

@dv, actually you can substitute _ with any valid variable name. The idea here is to take the [prop] of an object, encapsulate it inside the function and return only rest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment