How do I remove a key from a JavaScript object?
Let’s say we have an object with this format:
var thisIsObject = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
I wanted to do a function that removes by key:
removeFromObjectByKey('Cow');
What are the different methods to remove a property from a JavaScript object by its key name?
The delete operator is the most straightforward way to remove a key from a JavaScript object, while object destructuring with rest syntax provides an immutable alternative, and Reflect.deleteProperty() offers a more formalized modern approach. Each method has different characteristics regarding mutability, performance, and browser support that you should consider based on your specific use case.
Contents
- Using the delete Operator
- Object Destructuring with Rest Syntax
- Reflect.deleteProperty() Method
- Setting Properties to undefined
- Performance and Considerations
- Creating a Reusable Function
Using the delete Operator
The delete operator is the most direct and commonly used method to remove a property from a JavaScript object. It removes both the value of the property and the property itself from the object.
var thisIsObject = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
// Using delete operator
delete thisIsObject['Cow'];
// or delete thisIsObject.Cow;
console.log(thisIsObject);
// Output: { 'Cat': 'Meow', 'Dog': 'Bark' }
The delete operator returns true if the deletion was successful (including when the property doesn’t exist) and false if the property is configurable. As stated in the MDN documentation, the delete operator removes a property from an object.
Important considerations:
- The delete operator mutates the original object
- It cannot delete non-configurable properties
- Properties added with
var,let, orconstare non-configurable by default
Object Destructuring with Rest Syntax
Object destructuring with rest syntax offers a modern, immutable approach to removing properties from objects. This method creates a new object without the specified property while leaving the original object unchanged.
var thisIsObject = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
// Using object destructuring with rest
const { Cow, ...newObject } = thisIsObject;
console.log(newObject);
// Output: { 'Cat': 'Meow', 'Dog': 'Bark' }
console.log(thisIsObject);
// Output: { 'Cow': 'Moo', 'Cat': 'Meow', 'Dog': 'Bark' } (unchanged)
According to Ultimate Courses, this technique helps avoid mutable operations and the delete keyword, giving us an immutable pattern for removing properties from objects in JavaScript.
Key advantages:
- Immutable operation (original object remains unchanged)
- Clean, readable syntax
- Works well with modern JavaScript patterns
Limitations:
- Creates a new object (memory overhead)
- Not suitable for very large objects where performance is critical
Reflect.deleteProperty() Method
The Reflect.deleteProperty() method provides a more formalized and consistent way to delete properties. It’s part of the Reflect API introduced in ES6.
var thisIsObject = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
// Using Reflect.deleteProperty
const result = Reflect.deleteProperty(thisIsObject, 'Cow');
console.log(result); // true (successful deletion)
console.log(thisIsObject);
// Output: { 'Cat': 'Meow', 'Dog': 'Bark' }
As MDN documentation explains, this method returns true if the property was successfully deleted, and false otherwise. It essentially provides a function version of the delete operator with more consistent behavior.
Characteristics:
- Mutates the original object (like delete operator)
- More predictable return value than delete
- Can be used as a function reference in higher-order functions
- Better for functional programming patterns
Setting Properties to undefined
Another approach is to set the property value to undefined or null instead of deleting it entirely. This maintains the object’s structure while effectively removing the value.
var thisIsObject = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
// Setting to undefined
thisIsObject['Cow'] = undefined;
console.log(thisIsObject);
// Output: { 'Cow': undefined, 'Cat': 'Meow', 'Dog': 'Bark' }
// Or using null
thisIsObject.Cow = null;
When to use this approach:
- When you need to preserve the object’s structure
- When working with APIs that expect certain properties to exist
- When you want to distinguish between “deleted” and “undefined” states
Important note: This doesn’t actually remove the property from the object - it just sets its value to undefined. The property will still appear when iterating over the object’s keys.
Performance and Considerations
Different methods have different performance characteristics:
Performance Comparison
| Method | Mutates Original | Performance | Browser Support | Best For |
|---|---|---|---|---|
| delete | Yes | Good | All browsers | Quick deletion, legacy code |
| Destructuring | No | Slower | Modern browsers | Immutable operations, functional patterns |
| Reflect.deleteProperty | Yes | Similar to delete | Modern browsers | Consistent behavior, functional programming |
| Set undefined | Yes | Fastest | All browsers | Preserving structure, API compatibility |
According to Stack Overflow tests, destructuring can be slower than delete in some browsers, but the difference is often negligible for most applications.
Memory Considerations
- delete operator: Modifies existing object, minimal memory overhead
- Destructuring: Creates new object, higher memory usage for large objects
- Reflect.deleteProperty: Modifies existing object, similar memory to delete
- Set undefined: Modifies existing object, minimal memory overhead
Creating a Reusable Function
Here’s how to create the removeFromObjectByKey function using different approaches:
Using delete operator
function removeFromObjectByKey(obj, key) {
delete obj[key];
return obj;
}
// Usage
var thisIsObject = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
removeFromObjectByKey(thisIsObject, 'Cow');
console.log(thisIsObject);
// Output: { 'Cat': 'Meow', 'Dog': 'Bark' }
Using destructuring (immutable)
function removeFromObjectByKeyImmutable(obj, key) {
const { [key]: removed, ...rest } = obj;
return rest;
}
// Usage
var thisIsObject = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
var newObject = removeFromObjectByKeyImmutable(thisIsObject, 'Cow');
console.log(newObject);
// Output: { 'Cat': 'Meow', 'Dog': 'Bark' }
console.log(thisIsObject);
// Output: { 'Cow': 'Moo', 'Cat': 'Meow', 'Dog': 'Bark' } (unchanged)
Using Reflect.deleteProperty
function removeFromObjectByKeyReflect(obj, key) {
Reflect.deleteProperty(obj, key);
return obj;
}
// Usage
var thisIsObject = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};
removeFromObjectByKeyReflect(thisIsObject, 'Cow');
console.log(thisIsObject);
// Output: { 'Cat': 'Meow', 'Dog': 'Bark' }
Sources
- delete - JavaScript | MDN
- How do I remove a key from a JavaScript object? - Stack Overflow
- How to remove a property from a JavaScript object - GeeksforGeeks
- How To Remove a Property from a JavaScript Object - W3Schools
- Removing Object Properties with Destructuring - Ultimate Courses
- Reflect.deleteProperty() - JavaScript | MDN
- How to remove a property from an object in Javascript - CoreUI
- How to Remove a Property from a JavaScript Object - Scaler Topics
Conclusion
There are several effective methods to remove a key from a JavaScript object, each with its own advantages:
- The delete operator is the most straightforward and widely supported method, making it ideal for most use cases and legacy code
- Object destructuring with rest syntax provides an immutable approach that’s perfect for functional programming patterns and when you need to preserve the original object
- Reflect.deleteProperty() offers a more consistent and formal way to delete properties, with better behavior for functional programming
- Setting properties to undefined is useful when you need to maintain the object’s structure while effectively removing values
Choose the method that best fits your specific needs: use delete for performance-critical operations, destructuring for immutable patterns, Reflect for consistency, and undefined assignment for structure preservation. Consider your application’s performance requirements, browser compatibility needs, and whether immutability is important for your use case.