Created
March 1, 2019 07:27
-
-
Save joelnet/d8ef09c15d83485cf684e98ca1e55735 to your computer and use it in GitHub Desktop.
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
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, 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.
This gist is written for 7 Tricks with Resting and Spreading JavaScript Objects
@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?
@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
What does the underscore on line 6 actually stands for?