How to Remove an Entry by Key in JavaScript Object?
In JavaScript, objects store data in the form of key-value pairs where the key may be any property of the object. In this article let us see how to remove key-value pairs a by given key in the object.
Table of Content
1. Using the delete operator
When only a single key is to be removed we can directly use the delete operator specifying the key in an object.
Syntax:
delete(object_name.key_name);
/* or */
delete(object_name[key_name]);
Example 1: This example describes the above-explained approach to removing a key-value pair from an object.
let myObj = {
Name: "Raghav",
Age: 30,
Sex: "Male",
Work: "Web Developer",
YearsOfExperience: 6,
Organisation: "GeeksforGeeks",
Address: "address--address some value"
};
console.log("After removal: ");
// Deleting address key
delete (myObj.Address); // Or delete(myObj[Address]);
console.log(myObj);
Output
After removal: { Name: 'Raghav', Age: 30, Sex: 'Male', Work: 'Web Developer', YearsOfExperience: 6, Organisation: 'GeeksforGeeks' }
Example 2: This example uses a loop to remove a key-value pair from the object.
// Function to delete the keys given in the array
function DeleteKeys(myObj, array) {
for (let index = 0; index < array.length; index++) {
delete myObj[array[index]];
}
return myObj;
}
// Declaring the object
let myObj = {
Name: "Raghav",
Age: 30,
Sex: "Male",
Work: "Web Developer",
YearsOfExperience: 6,
Organisation: "Geeks For Geeks",
Address: "address--address some value"
};
// Adding the keys to be deleted in the array
let array =
["Work", "Address", "Organisation", "YearsOfExperience"];
let finalobj = DeleteKeys(myObj, array);
console.log("After removal: ");
console.log(finalobj);
Output
After removal: { Name: 'Raghav', Age: 30, Sex: 'Male' }
Using the filter() Method
The JavaScript Array filter() method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method.
Syntax:
array.filter(callback(element, index, arr), thisValue);
Example: In this example, we will see the use of the filter() method for removing the key-value pair.
let obj = { name: "Joey", age: "30", gender: "Male" };
let newObj = Object.keys(obj)
.filter(key => key != "name")
.reduce((acc, key) => {
acc[key] = obj[key];
return acc;
}, {});
console.log(newObj)
Output
{ age: '30', gender: 'Male' }
Using Destructuring and Object.assign
This method involves using object destructuring to exclude specific keys and then reassembling the remaining keys into a new object using Object.assign.
Syntax:
const { keyToRemove, ...rest } = object;
const newObject = Object.assign({}, rest);
Example: This example demonstrates how to use destructuring and Object.assign to remove a key-value pair from an object.
let myObj = {
Name: "Raghav",
Age: 30,
Sex: "Male",
Work: "Web Developer",
YearsOfExperience: 6,
Organisation: "GeeksforGeeks",
Address: "address--address some value"
};
// Function to delete a key using destructuring and Object.assign
function removeKey(obj, keyToRemove) {
const { [keyToRemove]: _, ...rest } = obj;
return Object.assign({}, rest);
}
let updatedObj = removeKey(myObj, "Address");
console.log("After removal:");
console.log(updatedObj);
Output
After removal: { Name: 'Raghav', Age: 30, Sex: 'Male', Work: 'Web Developer', YearsOfExperience: 6, Organisation: 'GeeksforGeeks' }
Using Object.fromEntries() with Object.entries()
This method involves converting the object into an array of key-value pairs, filtering out the entry to be removed, and then converting the filtered array back into an object.
Example:
let myObj = {
Name: "Raghav",
Age: 30,
Sex: "Male",
Work: "Web Developer",
YearsOfExperience: 6,
Organisation: "GeeksforGeeks",
Address: "address--address some value"
};
function removeKey(obj, keyToRemove) {
return Object.fromEntries(
Object.entries(obj).filter(([key]) => key !== keyToRemove)
);
}
let updatedObj = removeKey(myObj, "Address");
console.log("After removal:");
console.log(updatedObj);
Output
After removal: { Name: 'Raghav', Age: 30, Sex: 'Male', Work: 'Web Developer', YearsOfExperience: 6, Organisation: 'GeeksforGeeks' }