Created
October 16, 2019 14:50
-
-
Save ourmaninamsterdam/4484c373878c2f94e24a1d415074ccd2 to your computer and use it in GitHub Desktop.
Omit object property
This file contains hidden or 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 omit = (object, key) => { | |
const objectCopy = Object.assign({}, object); | |
Object.keys(objectCopy).forEach(itemKey => { | |
if(key === itemKey) { | |
delete objectCopy[itemKey]; | |
} | |
}) | |
return objectCopy; | |
}; | |
const justins = { | |
'1': {id: 1, name: 'Justin Bieber'}, | |
'2': {id: 2, name: 'Justin Timberlake'}, | |
'3': {id: 3, name: 'Justin Time'} | |
} | |
const lessJustins = omit(justins, '1'); | |
// { | |
// '2': {id: 2, name: 'Justin Timberlake'}, | |
// '3': {id: 3, name: 'Justin Time'} | |
// } | |
const oneJustin = omit(lessJustins, '2'); // { '3': {id: 3, name: 'Justin Time'} } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment