Explain Codes LogoExplain Codes Logo

How do I remove a key from a JavaScript object?

javascript
functions
promises
callbacks
Alex KataevbyAlex Kataev·Feb 13, 2025
TLDR

Here's your "magic spell" for banishing a key from a JavaScript object: the trusty delete operator:

let obj = { key: 'value' }; // obj is an innocent object with one poor-little key delete obj.key; // Boooom! The key has left the chat 😢 console.log(obj); // {} Like it never existed....

The delete wizardry whisks away the property altogether, so it conveniently ghosts you to undefined if you dare to call it afterward.

Removing dynamic keys

If your key name is something mysterious that's stored in a variable or you need your crystal ball to calculate it, [] bracket notation is the way to go:

let mySpookyKey = 'key'; delete obj[mySpookyKey]; // "Avada Kedavra!" to mySpookyKey

You'll find this trick especially crafty in functions when you need to perform some dark arts on objects based on changing conditions.

Preventing unintended obliteration

Before casting your delete incantation, it's highly recommended to confirm that your object is indeed haboring the property you're targeting. Else, you may end up with some unwelcome side effects:

if (obj.hasOwnProperty('key')) { delete obj.key; //Banishing the key only if it exists. Safety first, kids! }

Using good old hasOwnProperty makes sure you're not trying to excise properties from the object's ancestry line.

Alternatives to waving the delete wand

If you're someone who likes to keep things intact or work somewhere delete is frowned upon (React says "Hi!"), you might find these alternatives to your liking:

Conjuring a new object with the object spread and destructuring

let { key, ...rest } = obj; obj = {...rest}; // *Voila!* New object, same old treasures.

This approach creates a duplicate of your original, minus the key in question. No mutations here!

Calling upon the spirits of functional libraries

The Lodash's _.omit function is a neat trick:

obj = _.omit(obj, 'key'); // Easy peasy, lemon squeezy!

Cooking up a new object via reduce

obj = Object.keys(obj).reduce((acc, currentKey) => { if (currentKey !== 'key') acc[currentKey] = obj[currentKey]; return acc; }, {}); // Bippity boppity, your key is now exit('ing') property!

This magical method uses old man reduce to round up all the keys that didn't cross paths with 'delete' into a shiny new object.

Resistance against deletion

Certain keys might be possesed by the configurable attribute set to false, and may strike back when you try to delete them:

Object.defineProperty(obj, 'invincibleKey', { // resistant-to-deletion property configurable: false, // makes it invincible value: 'stays put' }); delete obj.invincibleKey; // returns false, invincibleKey laughs at your futile attempt

Conduct a post-mortem with Object.getOwnPropertyDescriptor to understand these paranormal activities.

When possession is better than exorcism

Sometimes, it's better to exile a property rather than eradicate it:

obj.key = undefined; // Sending the key to limbo

The key continues to haunt the object, but its value is as desolate as undefined. This practice maintains the spooky shape of the object, which might be handy in specific necromancy-like data transformations.