How to remove the key-value pairs corresponding to the given keys from an object using JavaScript ?

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 corresponding to a given key in the object.
- Using the delete operator
- Using the filter() method
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.
Javascript
| varmyObj = {    Name: "Raghav",    Age: 30,    Sex: "Male",    Work: "Web Developer",    YearsOfExperience: 6,    Organisation: "zambiatek",    Address: "address--address some value"};  console.log("After removal: ");// Deleting address keydelete(myObj.Address); // Or delete(myObj[Address]);console.log(myObj); | 
Output:
"After removal: "
[object Object] {
  Age: 30,
  Name: "Raghav",
  Organisation: "zambiatek",
  Sex: "Male",
  Work: "Web Developer",
  YearsOfExperience: 6
}
When multiple keys are to be removed then the keys can be stored in an array and can be passed to a function that uses a loop to delete the required keys in the array.
Syntax:
function function_name(object_name, array_of_keys) {
    { Iterate through the array using loop. }
    return object_name;
}
Example 2: This example uses a loop to remove a key-value pair from the object.
Javascript
| // Function to delete the keys given in the arrayfunctionDeleteKeys(myObj, array) {    for(let index = 0; index < array.length; index++) {        deletemyObj[array[index]];    }    returnmyObj;}  // Declaring the objectvarmyObj = {    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 arrayvararray =    ["Work", "Address", "Organisation", "YearsOfExperience"];varfinalobj = DeleteKeys(myObj, array);console.log("After removal: ");console.log(finalobj); | 
Output:
"After removal: "
[object Object] {
  Age: 30,
  Name: "Raghav",
  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.Â
Example: In this example, we will see the use of the filter() method for removing the key-value pair.
Javascript
| let obj = { name: "Joey", age: "30", gender: "Male"};let newObj = Object.keys(obj)    .filter(key => key != "name")    .reduce((acc, key) => {        acc[key] = obj[key];        returnacc;    }, {});  console.log(newObj) | 
{ age: '30', gender: 'Male' }
 
				 
					

