Skip to content

Instantly share code, notes, and snippets.

@joelnet
Created March 1, 2019 07:27
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
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!' }
@exoslav
Copy link

exoslav commented Mar 17, 2019

What does the underscore on line 6 actually stands for?

@meendoo
Copy link

meendoo commented Mar 18, 2019

@exoslav, he's assigning the computed [prop] to a new variable _ which is being ignore (that's the purpose here). Basically he's naming a property from the destructured object.

@joelnet
Copy link
Author

joelnet commented Mar 18, 2019

@DVGalarza
Copy link

@meendoo, Does that mean he could have also just used ({ [prop], ...rest }) as well, or is it required to put the whole property structure using a dummy value (_) in order to use a computed property?

@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