Skip to content

Instantly share code, notes, and snippets.

@ourmaninamsterdam
Created October 16, 2019 14:50
Show Gist options
  • Save ourmaninamsterdam/4484c373878c2f94e24a1d415074ccd2 to your computer and use it in GitHub Desktop.
Save ourmaninamsterdam/4484c373878c2f94e24a1d415074ccd2 to your computer and use it in GitHub Desktop.
Omit object property
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