How to clone object except for one or some keys

Let say you have an object that you want to use it as payload to make a request

const payload = {
‘username’: ‘Mark’,
‘Hash’ :’8fafasdf8afadsf’,
‘redirectUrl’:’/’
}

but you want to exclude redirectUrl from your object before make r…


This content originally appeared on DEV Community and was authored by Heru Hartanto

Let say you have an object that you want to use it as payload to make a request

const payload = {
    'username': 'Mark',
    'Hash' :'8fafasdf8afadsf',
    'redirectUrl':'/'
}

but you want to exclude redirectUrl from your object before make request, and you don't want to delete this key, hmmm it is easy doesn't it, just destructuring the object and rearrange it to a new variable

    const {username,Hash} = payload;
    const newPayload = {username,Hash}

but wait, what if your object is actually updated and now look like this

const payload = {
    'username': 'Mark',
    'Hash' :'8fafasdf8afadsf',
    'redirectUrl':'/',
    'firstname':'mark',
    'lastname':'brown',
    'birthdate':'01/12/2000',
    'gender':'MALE',
    'address':'planet earth'
}

seems like destructuring and rearrange is kind of hardwork to do.

"Put rest to the last" technique to the rescue

simply put keys that you don't want to use and put the rest of it in the last

const payload = {
    'username': 'Mark',
    'Hash' :'8fafasdf8afadsf',
    'redirectUrl':'/',
    'firstname':'mark',
    'lastname':'brown',
    'birthdate':'01/12/2000',
    'gender':'MALE',
    'address':'planet earth'
}
let{redirectUrl, ...newPayload} = payload
newPayload
/*
    {
        'username': 'Mark',
        'Hash' :'8fafasdf8afadsf',
        'firstname':'mark',
        'lastname':'brown',
        'birthdate':'01/12/2000',
        'gender':'MALE',
        'address':'planet earth'
    }
*/

If you want to add another key to exclude just simply put keys name after redirectUrl


This content originally appeared on DEV Community and was authored by Heru Hartanto


Print Share Comment Cite Upload Translate Updates
APA

Heru Hartanto | Sciencx (2021-04-24T06:06:55+00:00) How to clone object except for one or some keys. Retrieved from https://www.scien.cx/2021/04/24/how-to-clone-object-except-for-one-or-some-keys/

MLA
" » How to clone object except for one or some keys." Heru Hartanto | Sciencx - Saturday April 24, 2021, https://www.scien.cx/2021/04/24/how-to-clone-object-except-for-one-or-some-keys/
HARVARD
Heru Hartanto | Sciencx Saturday April 24, 2021 » How to clone object except for one or some keys., viewed ,<https://www.scien.cx/2021/04/24/how-to-clone-object-except-for-one-or-some-keys/>
VANCOUVER
Heru Hartanto | Sciencx - » How to clone object except for one or some keys. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/24/how-to-clone-object-except-for-one-or-some-keys/
CHICAGO
" » How to clone object except for one or some keys." Heru Hartanto | Sciencx - Accessed . https://www.scien.cx/2021/04/24/how-to-clone-object-except-for-one-or-some-keys/
IEEE
" » How to clone object except for one or some keys." Heru Hartanto | Sciencx [Online]. Available: https://www.scien.cx/2021/04/24/how-to-clone-object-except-for-one-or-some-keys/. [Accessed: ]
rf:citation
» How to clone object except for one or some keys | Heru Hartanto | Sciencx | https://www.scien.cx/2021/04/24/how-to-clone-object-except-for-one-or-some-keys/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.