- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript: How to remove the key-value pairs corresponding to the given keys from an object?
In JavaScript, objects can be created to store data as key-value pairs. The data in an object can be accessed using the dot notation (obj.key) or the bracket notation (obj["key"]). See the below example −
let obj = { key1: "value1", key2: "value2", key3: "value" };
We can remove the key-value pairs corresponding to the given keys from an object but in this tutorial, we are going to look at 3 methods.
Using the delete operator
The delete operator is used to delete a property of an object. The delete operator will not delete the variable itself, but only the value of the variable.
Example
See the below example −
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result"></div> <script> let obj = { key1: "value1", key2: "value2", key3: "value3" }; delete obj.key2; document.getElementById("result").innerHTML = JSON.stringify(obj); console.log(obj) </script> </body> </html>
As you can see from the above example, the delete operator only deletes the value of the key and not the key itself.
Below is the line by line explanation of the above code −
let obj = { key1: "value1", key2: "value2", key3: "value3" };
We have created an object with 3 key-value pairs.
delete obj.key2;
The delete operator is used to delete the key-value pair where the key is “key2”.
console.log(obj);
The output of the above code in the console will be: { key1: "value1", key3: "value3" }. As you can see, the key-value pair with the key “key2” is deleted from the object.
Using the filter() method
The filter() method is used to create a new array from an existing array. See the below example:
Example
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result"></div> <script> let obj = { key1: "value1", key2: "value2", key3: "value3" }; let newObj = Object.keys(obj) .filter(key => key != "key2") .reduce((acc, key) => { acc[key] = obj[key]; return acc; }, {}); document.getElementById("result").innerHTML = JSON.stringify(newObj); console.log(newObj) </script> </body> </html>
As you can see from the above example, the filter() method only deletes the value of the key and not the key itself.
Below is the line by line explanation of the above code:
let obj = { key1: "value1", key2: "value2", key3: "value3" };
We have created an object with 3 key-value pairs.
let newObj = Object.keys(obj) .filter(key => key != "key2") .reduce((acc, key) => { acc[key] = obj[key]; return acc; }, {});
The Object.keys() method is used to create an array of the keys of the object. The filter() method is used to create a new array from the existing array. The key is compared with “key2”. If it is not equal, then the key-value pair is added to the new array. The reduce() method is used to reduce the array to an object.
console.log(newObj);
The output of the above code will be: { key1: "value1", key3: "value3" }. As you can see, the key-value pair with the key “key2” is deleted from the object.
Using the for…in loop
The for…in loop is used to iterate over the properties of an object.
Example
See the below example −
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result"></div> <script> let obj = { key1: "value1", key2: "value2", key3: "value3" }; for (let key in obj) { if (key == "key2") { delete obj[key]; } } document.getElementById("result").innerHTML = JSON.stringify(obj); console.log(obj) </script> </body> </html>
As you can see from the above example, the for…in loop only deletes the value of the key and not the key itself.
Below line by-line explanation of the above code:
let obj = {key1: "value1", key2: "value2", key3: "value3"};
We have created an object with 3 key-value pairs.
for (let key in obj) { if (key == "key2") { delete obj[key]; } }
The for…in loop is used to iterate over the properties of an object. The key variable is used to store the key of the object. If the key is “key2”, then the key-value pair is deleted from the object.
console.log(obj);
The output of the above code will be: { key1: "value1", key3: "value3" }. As you can see, the key-value pair with the key “key2” is deleted from the object.
Conclusion
In this tutorial, we looked at 3 methods to remove the key-value pairs corresponding to the given keys from an object. The delete operator, the for…in loop and the filter() method.
- Related Articles
- JavaScript: How to Create an Object from Key-Value Pairs
- How to extract subset of key-value pairs from Python dictionary object?
- How to modify key values in an object with JavaScript and remove the underscore?
- How to access an object value using variable key in JavaScript?
- JavaScript map value to keys (reverse object mapping)
- How to get key name when the value contains empty in an object with JavaScript?
- How to unflatten an object with the paths for keys in JavaScript?
- How to remove an item from JavaScript array by value?
- How to remove all blank objects from an Object in JavaScript?
- Iterate through Object keys and manipulate the key values in JavaScript
- How to sort an object in ascending order by the value of a key in JavaScript?
- How to workaround Objects vs arrays in JavaScript for key/value pairs?
- How to set dynamic property keys to an object in JavaScript?
- Maps in JavaScript takes keys and values array and maps the values to the corresponding keys
- Java Program to remove key value pair from HashMap?
